ld library path not working under OS X 10.9

后端 未结 3 1540
执笔经年
执笔经年 2021-01-23 06:03

I\'ve been trying to figure out why g++ cannot link a program with the armadillo library. The problem is simple:

macbook-pro:arma-xc jmlopez$ g++-4.         


        
相关标签:
3条回答
  • 2021-01-23 06:24

    The reason for this is that Xcode sets the SDK (in your case the macOS SDK) as the virtual root. So all search paths of the compiler and the linker are being interpreted as relative to the SDK folder.

    Specifically, the Linker will not look in /usr/lib, it will look in [macOS SDK]/usr/lib.

    If you need to use /usr/lib (e.g. when linking against OpenSSL's libssl which only exist in /usr/lib but not in [macOS SDK]/usr/lib, you can set "-isysroot /" as the first two "Other Linker Flags" of your project's or target's build settings. This ensures that the Linker will use / as the root (and hence will find libraries in /usr/lib), while the compiler will continue to use the virtual root set by Xcode (i.e. the macOS SDK).

    0 讨论(0)
  • 2021-01-23 06:25

    I ran into this thread trying to answer a similar question myself. Hopefully this will help out.

    You need ld_library_path for the homebrew compiler to work and dyld_library_path for os x to appropriately use the library.

    put the following exports somewhere where they can get sourced.

    export LD_LIBRARY_PATH=/usr/lib:$LD_LIBRARY_PATH

    export DYLD_LIBRARY_PATH=$LD_LIBRARY_PATH:$DYLD_LIBRARY_PATH

    This worked for me when compiling with homebrew gcc on OS X 10.9 and 10.10.

    0 讨论(0)
  • 2021-01-23 06:39

    It is likely that your homebrew g++ was compiled with a custom prefix. The prefix for gcc is usually /usr, meaning that it will look for binaries in /usr/bin, headers in /usr/include and libraries in /usr/lib. If you've compiled with a custom prefix (/usr/local/Cellar/gcc/4.9.1/ in this case, it seems) the it won't look in /usr/lib.

    LD_LIBRARY_PATH is how you tell the runtime linker where to look for libraries. So if you had linked to /usr/my/bizarre/lib/path/libmylib.so, then you run it like this:

    > LD_LIBRARY_PATH=/usr/my/bizarre/lib/path myprog
    

    To tell g++ where to find libraries, you use the -L command-line option. So your command-line should look like this:

    g++-4.9 inputs-arma.cpp -L/usr/lib -larmadillo
    
    0 讨论(0)
提交回复
热议问题