GithubHelp home page GithubHelp logo

Comments (14)

nopeslide avatar nopeslide commented on July 17, 2024 1

@alrevuelta will look into it.

from connxr.

nopeslide avatar nopeslide commented on July 17, 2024

I tried the mechanism with a minimal example and it works:

main.c

#include <stdio.h>

extern __attribute__((weak)) char* func(void);

int main() {

    printf("func @ %p\n",func);

    if (func) {
        printf("func: %s\n", func());
    } else {
        printf("func not defined\n");
    }
    return 0;
}

func.c

char* func(void) {
    return "hello";
}
$ gcc -std=c99 -c main.c -o main.o
$ gcc -std=c99 -c func.c -o func.o
$ gcc -std=c99 main.o -o no_func
$ ./no_func 
func @ (nil)
func not defined
$ gcc -std=c99 main.o func.o -o func
$ ./func   
func @ 0x55ff463ed1a2
func: hello

the extern __attribute__((weak)) line actually produces a function pointer, which is by default NULL. when the linker finds a matching function it "updates" the pointer.

from connxr.

nopeslide avatar nopeslide commented on July 17, 2024

ah sorry, I misunderstood the problem

from connxr.

nopeslide avatar nopeslide commented on July 17, 2024

the address operators inside the return statements are wrong. the resolver should return the function pointer itself, not the address of the function pointer. will update the generator.

from connxr.

alrevuelta avatar alrevuelta commented on July 17, 2024

@nopeslide
Actually what I meant was your first reply. The third command is not working for me under macOS:

$ gcc -std=c99 main.o -o no_func

Undefined symbols for architecture x86_64:
  "_func", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

After a quick search I found this where one of the solutions suggests to add the following flags -Wl,-flat_namespace,-undefined,dynamic_lookup and that seems to work for me.

from connxr.

nopeslide avatar nopeslide commented on July 17, 2024

After a quick search I found this where one of the solutions suggests to add the following flags -Wl,-flat_namespace,-undefined,dynamic_lookup and that seems to work for me.

these flags are not present in my ld. are these mac specific flags?

from connxr.

alrevuelta avatar alrevuelta commented on July 17, 2024

Looks like its mac specific. Seems also that its not really a good idea.

"Weak linking with weak_import really only works well with dynamic libraries. You can get it to work with static linking (by specifying -undefined dynamic_lookup as suggested above) but this isn't such a hot idea. It means that no undefined symbols will be detected until runtime. This is something I would avoid in production code, personally."

Will look further into this.

from connxr.

nopeslide avatar nopeslide commented on July 17, 2024

since we are building a static binary no symbol search should be done at runtime. could you pass the static flag and see if it still does not work or if flat_namespace is still needed?

from connxr.

nopeslide avatar nopeslide commented on July 17, 2024

are you developing on a mac? if not we could keep these flags and just trust the linux gcc to report any linker errors

from connxr.

alrevuelta avatar alrevuelta commented on July 17, 2024

If you mean something like:

gcc -std=c99 main.o -o no_func -static

I'm geetting:

ld: library not found for -lcrt0.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)

And according to gcc man:

This option will not work on Mac OS X unless all libraries (including libgcc.a) have also been compiled with -static. Since neither a static version of libSystem.dylib nor crt0.o are provided, this option is not useful to most people.

Yep, I'm developing on mac.

from connxr.

nopeslide avatar nopeslide commented on July 17, 2024

I looked at a few other projects with Mac ports (i.e. gcc). it seems they're all using these flags.
Could you try if the linker accepts missing hard refs with these flags by adding a function prototype and using it without defining the actual function?
If so, this would be a problem, if not we may use them.
Alternatively I could rewrite the Makefile to partially link everything operator related with these flags.
Hopefully this would allow "normal" linking of everything else

from connxr.

alrevuelta avatar alrevuelta commented on July 17, 2024

You mean these flags? -Wl,-flat_namespace,-undefined,dynamic_lookup?

from connxr.

nopeslide avatar nopeslide commented on July 17, 2024

yes

from connxr.

alrevuelta avatar alrevuelta commented on July 17, 2024

Solved in #34

from connxr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.