command-line library build fails with linker error

前端 未结 2 1186
温柔的废话
温柔的废话 2020-12-18 07:03

I am getting a library not found error building GraphViz current release (June 7 2012) with Xcode 4.3 using a script. I may have made mistakes updating build script

相关标签:
2条回答
  • 2020-12-18 07:33

    The compiler normally uses crt1.o combined with crt[i/n].o and crt[begin/end].o to support the constructors and destructors (functions called before and after main and exit).

    This error could be caused by this missing library file for the specific deployment target.

    First, do some investigation, like:

    1. list all your deployment targets:

      ls -la /Developer/SDKs

    2. and find which crt1 libraries do you have for which environment

      find /Developer/SDKs -name crt1\*

    You could see something like:

    /Developer/SDKs/MacOSX10.5.sdk/usr/lib/crt1.10.5.o
    /Developer/SDKs/MacOSX10.5.sdk/usr/lib/crt1.o
    /Developer/SDKs/MacOSX10.6.sdk/usr/lib/crt1.10.5.o
    /Developer/SDKs/MacOSX10.6.sdk/usr/lib/crt1.10.6.o
    /Developer/SDKs/MacOSX10.6.sdk/usr/lib/crt1.o
    

    So as you can see, crt1.10.6.o is missing for MacOSX10.5.

    Solution 1:

    You can solve that by creating the link to the missing file pointed to the other environment, or you could change your deployment target. E.g.

    ln -s /Developer/SDKs/MacOSX10.6.sdk/usr/lib/crt1.10.6.o /Developer/SDKs/MacOSX10.5.sdk/usr/lib/
    

    Also this could be caused, that you have different gcc installed in your system. See:

    which gcc;

    xcrun -find gcc;

    brew list | grep gcc; brew list gcc47

    Solution 2

    So when you're compiling using make, you can actually specify the right compiler by CC variable. E.g.

    CC=/path/to/gcc-3.4 make
    

    Solution 3

    What you can also try is specifying the right target deployment environment variable for gcc, by executing the following lines:

    export MACOSX_DEPLOYMENT_TARGET=10.5
    export C_INCLUDE_PATH=/Developer/SDKs/MacOSX10.5.sdk/usr/include
    export LIBRARY_PATH=/Developer/SDKs/MacOSX10.5.sdk/usr/lib
    

    If this works, then you can add above lines to your shell profile (~/.profile) to make the change permanent.


    How to test

    Create the example conftest.c file with the following code:

    #ifdef __GNUC__
      yes;
    #endif
    

    And try to compile it via:

    gcc conftest.c
    cc conftest.c
    cc conftest.cc conftest.c
    

    Troubleshooting

    To see which exactly what file is missing, try to debug it using dtruss, e.g.:

    sudo dtruss -f gcc conftest.c 2>/dev/stdout | grep crt
    

    You should see something like:

    12426/0xb4e3b:  stat64("/Developer/usr/lib/gcc/i686-apple-darwin10/4.2.1/crt1.10.6.o\0", 0x7FFF5FBFE780, 0xB)        = -1 Err#2
    

    So once you found the missing file, then you can follow by the first solution by linking the missing file from existing location (e.g. locate crt1.10.6.o). If you will have other missing symbols, then try another file (check the architecture before by: file `locate crt1.10.6.o`).

    E.g.

    sudo ln -s /Applications/Xcode.app/Contents/Developer//Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/lib/crt1.10.6.o /Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2/lib/gcc/i686-apple-darwin11/4.2.1/crt1.10.6.o
    sudo ln -s /Applications/Xcode.app/Contents/Developer//Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/lib/crt1.10.6.o /Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2/lib/gcc/i686-apple-darwin11/4.2.1/x86_64/crt1.10.6.o
    

    Related

    Error in xcode project: ld: library not found for -lcrt1.10.6.o

    0 讨论(0)
  • 2020-12-18 07:36

    If I remember correctly this is what fixed the library not found problem.

    CFLAGS="$(OTHER_CFLAGS) -miphoneos-version-min=5.0"
    LDFLAGS="$(OTHER_LDFLAGS) --miphoneos-version-min=5.0"
    

    To link this to Xcode, under Build Settings then Header and Library search paths you need to add the paths to the built versions of the library and the header.

    You can add the build script as part of your Xcode project, but I haven't had success with this, plus you should only need to build it once per version, so putting the time into anything other than a build script doesn't have much return.

    If you decide to put the script in your project anyway (good luck!), then go to the build phases tab, add a build phase of type "Run Script" and paste your script there.

    0 讨论(0)
提交回复
热议问题