Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to implement gcc __attribute__((constructor)) when compiling dynamic libraries under Linux #110102

Open
aadog opened this issue Nov 23, 2024 · 2 comments
Labels
area-NativeAOT-coreclr untriaged New issue has not been triaged by the area owner

Comments

@aadog
Copy link

aadog commented Nov 23, 2024

Description

How to implement gcc attribute((constructor)) when compiling dynamic libraries under Linux

Reproduction Steps

1.create classlib

2.dotnet publish -r linux-bionic-arm64 -p:DisableUnsupportedError=true -p:PublishAotUsingRuntimePack=true -p:RemoveSections=true

Expected behavior

gcc attribute((constructor))

Actual behavior

no init

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Nov 23, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Nov 23, 2024
@huoyaoyuan huoyaoyuan added area-NativeAOT-coreclr and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Nov 23, 2024
Copy link
Contributor

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas
See info in area-owners.md if you want to be subscribed.

@am11
Copy link
Member

am11 commented Nov 23, 2024

You can use module initializer for this purpose.

Publish a project with module initializer:

$ dotnet new classlib -n ModInitLib
$ cd ModInitLib
$ cat > Class1.cs <<EOF
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

public static class LibraryInitializer
{
    [ModuleInitializer]
    public static void Initialize()
    {
        Console.WriteLine("Library has been initialized (ModuleInitializer)!");
    }

    [UnmanagedCallersOnly(EntryPoint = "say_hello")]
    public static void SayHello()
    {
        Console.WriteLine("Hello from ModInitLib!");
    }
}
EOF

$ dotnet publish -p:PublishAot=true -p:TargetName=libModInitLib -o dist --ucr

Link with some executable code:

$ cat > glue.c <<EOF
#include <stdio.h>

void say_hello();

int main(void)
{
    say_hello();
    return 0;
}
EOF

$ cc -o glue glue.c -L./dist -lModInitLib
$ ./glue
Library has been initialized (ModuleInitializer)!
Hello from ModInitLib!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-NativeAOT-coreclr untriaged New issue has not been triaged by the area owner
Projects
Status: No status
Development

No branches or pull requests

3 participants