ld library path not working under OS X 10.9 - c++

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.9 inputs-arma.cpp -larmadillo
ld: library not found for -larmadillo
collect2: error: ld returned 1 exit status
Perhaps I did specify where the library should be
macbook-pro:arma-xc jmlopez$ ls -l /usr/lib/libarma*
-rwxr-xr-x 1 root wheel 37928 Aug 18 14:30 /usr/lib/libarmadillo.4.32.2.dylib
lrwxr-xr-x 1 root wheel 25 Aug 18 14:30 /usr/lib/libarmadillo.4.dylib -> libarmadillo.4.32.2.dylib
lrwxr-xr-x 1 root wheel 20 Aug 18 14:30 /usr/lib/libarmadillo.dylib -> libarmadillo.4.dylib
Let us try again
macbook-pro:arma-xc jmlopez$ g++-4.9 -L/usr/lib inputs-arma.cpp -larmadillo
ld: library not found for -larmadillo
collect2: error: ld returned 1 exit status
Ok, that did not fix it. Let us see what is going on, for this we will use the -v option which will generate some long output.
macbook-pro:arma-xc jmlopez$ g++-4.9 -L/usr/lib inputs-arma.cpp -larmadillo -v
Using built-in specs.
COLLECT_GCC=g++-4.9
COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc/4.9.1/libexec/gcc/x86_64-apple-darwin13.3.0/4.9.1/lto-wrapper
Target: x86_64-apple-darwin13.3.0
Configured with: ../configure --build=x86_64-apple-darwin13.3.0 --prefix=/usr/local/Cellar/gcc/4.9.1 --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-4.9 --with-gmp=/usr/local/opt/gmp --with-mpfr=/usr/local/opt/mpfr --with-mpc=/usr/local/opt/libmpc --with-cloog=/usr/local/opt/cloog --with-isl=/usr/local/opt/isl --with-system-zlib --enable-version-specific-runtime-libs --enable-libstdcxx-time=yes --enable-stage1-checking --enable-checking=release --enable-lto --disable-werror --with-pkgversion='Homebrew gcc 4.9.1' --with-bugurl=https://github.com/Homebrew/homebrew/issues --enable-plugin --disable-nls --enable-multilib --with-native-system-header-dir=/usr/include --with-sysroot=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk
Thread model: posix
gcc version 4.9.1 (Homebrew gcc 4.9.1)
COLLECT_GCC_OPTIONS='-mmacosx-version-min=10.9.3' '-L/usr/lib' '-v' '-shared-libgcc' '-mtune=core2'
/usr/local/Cellar/gcc/4.9.1/libexec/gcc/x86_64-apple-darwin13.3.0/4.9.1/cc1plus -quiet -v -D__DYNAMIC__ inputs-arma.cpp -fPIC -quiet -dumpbase inputs-arma.cpp -mmacosx-version-min=10.9.3 -mtune=core2 -auxbase inputs-arma -version -o /var/folders/6x/ss29s2r51z5cv0_g1ffx0c0c0000gn/T//ccApA95Q.s
GNU C++ (Homebrew gcc 4.9.1) version 4.9.1 (x86_64-apple-darwin13.3.0)
compiled by GNU C version 4.9.1, GMP version 6.0.0, MPFR version 3.1.2-p8, MPC version 1.0.2
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/local/include"
ignoring nonexistent directory "/usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1/../../../../x86_64-apple-darwin13.3.0/include"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
/Users/jmlopez/github/excentury/excentury/extern/include
/usr/include
/usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1/include/c++
/usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1/include/c++/x86_64-apple-darwin13.3.0
/usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1/include/c++/backward
/usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1/include
/usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1/include-fixed
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/System/Library/Frameworks
End of search list.
GNU C++ (Homebrew gcc 4.9.1) version 4.9.1 (x86_64-apple-darwin13.3.0)
compiled by GNU C version 4.9.1, GMP version 6.0.0, MPFR version 3.1.2-p8, MPC version 1.0.2
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 5a051bcf2be886a5c6eb8ba33f338693
COLLECT_GCC_OPTIONS='-mmacosx-version-min=10.9.3' '-L/usr/lib' '-v' '-shared-libgcc' '-mtune=core2'
as -arch x86_64 -force_cpusubtype_ALL -o /var/folders/6x/ss29s2r51z5cv0_g1ffx0c0c0000gn/T//cchm9rVF.o /var/folders/6x/ss29s2r51z5cv0_g1ffx0c0c0000gn/T//ccApA95Q.s
COMPILER_PATH=/usr/local/Cellar/gcc/4.9.1/libexec/gcc/x86_64-apple-darwin13.3.0/4.9.1/:/usr/local/Cellar/gcc/4.9.1/libexec/gcc/x86_64-apple-darwin13.3.0/4.9.1/:/usr/local/Cellar/gcc/4.9.1/libexec/gcc/x86_64-apple-darwin13.3.0/:/usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1/:/usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/
LIBRARY_PATH=/usr/lib/:/usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1/:/usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1/../../../:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/lib/
COLLECT_GCC_OPTIONS='-mmacosx-version-min=10.9.3' '-L/usr/lib' '-v' '-shared-libgcc' '-mtune=core2'
/usr/local/Cellar/gcc/4.9.1/libexec/gcc/x86_64-apple-darwin13.3.0/4.9.1/collect2 -dynamic -arch x86_64 -macosx_version_min 10.9.3 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -weak_reference_mismatches non-weak -o a.out -L/usr/lib -L/usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1 -L/usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1/../../.. -L/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/lib /var/folders/6x/ss29s2r51z5cv0_g1ffx0c0c0000gn/T//cchm9rVF.o -larmadillo -lstdc++ -no_compact_unwind -lSystem -lgcc_ext.10.5 -lgcc -lSystem -v
collect2 version 4.9.1
/usr/bin/ld -dynamic -arch x86_64 -macosx_version_min 10.9.3 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -weak_reference_mismatches non-weak -o a.out -L/usr/lib -L/usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1 -L/usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1/../../.. -L/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/lib /var/folders/6x/ss29s2r51z5cv0_g1ffx0c0c0000gn/T//cchm9rVF.o -larmadillo -lstdc++ -no_compact_unwind -lSystem -lgcc_ext.10.5 -lgcc -lSystem -v
#(#)PROGRAM:ld PROJECT:ld64-236.4
configured to support archs: armv6 armv7 armv7s arm64 i386 x86_64 armv6m armv7m armv7em
Library search paths:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/lib
/usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1
/usr/local/Cellar/gcc/4.9.1/lib
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/lib
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/lib
Framework search paths:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/System/Library/Frameworks/
ld: library not found for -larmadillo
collect2: error: ld returned 1 exit status
The important information is here:
Library search paths:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/lib
/usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1
/usr/local/Cellar/gcc/4.9.1/lib
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/lib
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/lib
Somehow it is not looking in /usr/lib. I have tried specifying this directory by first
exporting the variable LD_LIBRARY_PATH
macbook-pro:arma-xc jmlopez$ export LD_LIBRARY_PATH=/usr/lib
macbook-pro:arma-xc jmlopez$ g++-4.9 -L/usr/lib inputs-arma.cpp -larmadillo
ld: library not found for -larmadillo
collect2: error: ld returned 1 exit status
No luck. Finally, thinking that ld might be broken I did this:
macbook-pro:arma-xc jmlopez$ ln -s /usr/lib/libarmadillo.dylib /usr/local/Cellar/gcc/4.9.1/lib/libarmadillo.dylib
macbook-pro:arma-xc jmlopez$ g++-4.9 inputs-arma.cpp -larmadillo
Now it works. I'm not sure if there is something wrong with my system but it seems that I have no way of specifying other paths for ld to look for my libraries. Instead I have to use any of the following:
Library search paths:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/lib
/usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1
/usr/local/Cellar/gcc/4.9.1/lib
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/lib
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/lib
Is there a way to fix this? Notice that I'm using the g++ from homebrew since I thought that the one that came with xcode was broken. In any case, no luck, at least the homebrew g++ told me the paths where the library was being searched.
Update:
I decided to see if I could add another path:
g++-4.9 -L/Users/jmlopez/ -L/Users/jmlopez/Desktop -L/usr/lib inputs-arma.cpp -larmadillo -v
Now the list of paths where it searches for the libraries is:
Library search paths:
/Users/jmlopez/
/Users/jmlopez/Desktop
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/lib
/usr/local/Cellar/gcc/4.9.1/lib/gcc/x86_64-apple-darwin13.3.0/4.9.1
/usr/local/Cellar/gcc/4.9.1/lib
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/lib
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/lib
Where is /usr/lib? What is so special about this directory that ld refuses to look in this directory. It searches in my home directory and in my Desktop. I tried adding /usr/local and it adds it to the list of directories. I'm now starting to wonder about the permissions on the directories... Any ideas?

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

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.

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).

Related

How to compile C++ using MinGW on Windows?

I'm getting the following error
cannot find -lstdc++
collect2.exe: error: ld returned 1 exit status
it's a simple Hello World program.
g++ version returns
g++ (MinGW.org GCC Build-2) 9.2.0
g++ -v <filename.cpp> returns
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/9.2.0/lto-wrapper.exe
Target: mingw32
Configured with: ../src/gcc-9.2.0/configure --build=x86_64-pc-linux-gnu --host=mingw32 --target=mingw32 --disable-win32-registry --with-arch=i586 --with-tune=generic --enable-static --enable-shared --enable-threads --enable-languages=c,c++,objc,obj-c++,fortran,ada --with-dwarf2 --disable-sjlj-exceptions --enable-version-specific-runtime-libs --enable-libgomp --disable-libvtv --with-libiconv-prefix=/mingw --with-libintl-prefix=/mingw --enable-libstdcxx-debug --disable-build-format-warnings --prefix=/mingw --with-gmp=/mingw --with-mpfr=/mingw --with-mpc=/mingw --with-isl=/mingw --enable-nls --with-pkgversion='MinGW.org GCC Build-2'
Thread model: win32
gcc version 9.2.0 (MinGW.org GCC Build-2)
COLLECT_GCC_OPTIONS='-v' '-shared-libgcc' '-mtune=generic' '-march=i586'
c:/mingw/bin/../libexec/gcc/mingw32/9.2.0/cc1plus.exe -quiet -v -iprefix c:\mingw\bin\../lib/gcc/mingw32/9.2.0/ hw.cpp -quiet -dumpbase hw.cpp -mtune=generic -march=i586 -auxbase hw -version -o C:\Users\move78~1\AppData\Local\Temp\ccQus9Kp.s
GNU C++14 (MinGW.org GCC Build-2) version 9.2.0 (mingw32)
compiled by GNU C version 9.2.0, GMP version 6.1.2, MPFR version 4.0.2, MPC version 1.1.0, isl version isl-0.21-GMP
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "c:\mingw\bin\../lib/gcc/mingw32/9.2.0/../../../../mingw32/include"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/9.2.0/include/c++"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/9.2.0/include/c++/mingw32"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/9.2.0/include/c++/backward"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/9.2.0/include"
ignoring duplicate directory "/mingw/lib/gcc/mingw32/9.2.0/../../../../include"
ignoring duplicate directory "c:/mingw/lib/gcc/../../include"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/9.2.0/include-fixed"
ignoring nonexistent directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/9.2.0/../../../../mingw32/include"
ignoring duplicate directory "/mingw/include"
#include "..." search starts here:
#include <...> search starts here:
c:\mingw\bin\../lib/gcc/mingw32/9.2.0/include/c++
c:\mingw\bin\../lib/gcc/mingw32/9.2.0/include/c++/mingw32
c:\mingw\bin\../lib/gcc/mingw32/9.2.0/include/c++/backward
c:\mingw\bin\../lib/gcc/mingw32/9.2.0/include
c:\mingw\bin\../lib/gcc/mingw32/9.2.0/../../../../include
c:\mingw\bin\../lib/gcc/mingw32/9.2.0/include-fixed
End of search list.
GNU C++14 (MinGW.org GCC Build-2) version 9.2.0 (mingw32)
compiled by GNU C version 9.2.0, GMP version 6.1.2, MPFR version 4.0.2, MPC version 1.1.0, isl version isl-0.21-GMP
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: cb31672760a9c1b42d1ddb86616116cd
COLLECT_GCC_OPTIONS='-v' '-shared-libgcc' '-mtune=generic' '-march=i586'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/as.exe -v -o C:\Users\move78~1\AppData\Local\Temp\cctjBjko.o C:\Users\move78~1\AppData\Local\Temp\ccQus9Kp.s
GNU assembler version 2.32 (mingw32) using BFD version (GNU Binutils) 2.32
COMPILER_PATH=c:/mingw/bin/../libexec/gcc/mingw32/9.2.0/;c:/mingw/bin/../libexec/gcc/;c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/
LIBRARY_PATH=c:/mingw/bin/../lib/gcc/mingw32/9.2.0/;c:/mingw/bin/../lib/gcc/;c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/lib/;c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../
COLLECT_GCC_OPTIONS='-v' '-shared-libgcc' '-mtune=generic' '-march=i586'
c:/mingw/bin/../libexec/gcc/mingw32/9.2.0/collect2.exe -plugin c:/mingw/bin/../libexec/gcc/mingw32/9.2.0/liblto_plugin-0.dll -plugin-opt=c:/mingw/bin/../libexec/gcc/mingw32/9.2.0/lto-wrapper.exe -plugin-opt=-fresolution=C:\Users\move78~1\AppData\Local\Temp\ccxROszG.res -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmsvcrt -plugin-opt=-pass-through=-ladvapi32 -plugin-opt=-pass-through=-lshell32 -plugin-opt=-pass-through=-luser32 -plugin-opt=-pass-through=-lkernel32 -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmsvcrt -Bdynamic -u ___register_frame_info -u ___deregister_frame_info c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../crt2.o c:/mingw/bin/../lib/gcc/mingw32/9.2.0/crtbegin.o -Lc:/mingw/bin/../lib/gcc/mingw32/9.2.0 -Lc:/mingw/bin/../lib/gcc -Lc:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/lib -Lc:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../.. C:\Users\move78~1\AppData\Local\Temp\cctjBjko.o -lstdc++ -lmingw32 -lmingwex -lmingw32 -lmingwex -lgcc_s -lgcc -lmoldname -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lmingwex -lmingw32 -lmingwex -lgcc_s -lgcc -lmoldname -lmsvcrt c:/mingw/bin/../lib/gcc/mingw32/9.2.0/crtend.o
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find -lstdc++
collect2.exe: error: ld returned 1 exit status
You need 3 things to build:
Your tools (like g++.exe) need to be in your PATH (or specified via their full path when calling them, e.g. C:\mingw\mingw32\bin\g++.exe).
The compiler needs to be able to find the include files. If needed you can add include paths (pointing to the folder, not the actual file) using -I flags (or you can use C_INCLUDE_PATH (for C) and CPLUS_INCLUDE_PATH (for C++) environment variables).
The linker needs be able to find your libraries (like libstdc++.a). If needed you can add library paths (pointing to the folder, not the actual file) using -L flags (or you can use LIBRARY_PATH environment variable).
If you use MSYS2 shell these things should already have been set up.
The standalone build from http://winlibs.com/ looks for relative locations and should just work wherever you extract it on your disk.
You didn't mention which shell you were running your commands from (Command Prompt, PowerShell, MSYS), but it's important the above is respected correctly in whatever shell you are using.
As far as I know http://winlibs.com/ works in MSYS shell, Command Prompt and PowerShell (though parameters starting with - need to be put in quotes when using PowerShell).

How to mix C++ and Fortran using CMake -G Xcode?

I created a simple C++ file where I call a Fortran subroutine. Using CMake without the Xcode IDE everything compiles just fine, whereas trying to add the Xcode IDE with the command:
cmake .. -G Xcode
returns the following error:
Compilation of the Fortran compiler identification source "CMakeFortranCompilerId.F" did not produce an executable in ...
Somebody can give me a hint on how to debug this one?
The source files are given by:
CMakeLists.txt
cmake_minimum_required(VERSION 3.7.0)
project(mixing CXX Fortran)
add_executable(mixing
testC.cpp
testF.f
)
testC.cpp
#include <iostream>
using namespace std;
extern"C" {
void fortfunc_(int *ii, float *ff);
}
int main()
{
int ii=5;
float ff=5.5;
fortfunc_(&ii, &ff);
return 0;
}
testF.f
subroutine fortfunc(ii,ff)
integer ii
real*4 ff
write(6,100) ii, ff
100 format('ii=',i2,' ff=',f6.3)
return
end
CMakeFiles/CMakeError.log
Compilation of the Fortran compiler identification source "CMakeFortranCompilerId.F" did not produce an executable in "/Users/Knowledge/Developer/Tutorials/MixingFortranAndC/XCode_CMake_FortranMixing/build/CMakeFiles/3.7.0/CompilerIdFortran".
CMakeFiles/CMakeOutput.log
bins
cmake .. -G Xcode
cmake ..
difference between the two files
Compiling the Fortran compiler identification source file "CMakeFortranCompilerId.F" succeeded.
Compiler:
Build flags:
Id flags:
The output was:
0
=== BUILD TARGET CompilerIdFortran OF PROJECT CompilerIdFortran WITH THE DEFAULT CONFIGURATION (Debug) ===
Check dependencies
warning: no rule to process file '/Users/Knowledge/Developer/Tutorials/MixingFortranAndC/XCode_CMake_FortranMixing/build/CMakeFiles/3.7.0/CompilerIdFortran/CMakeFortranCompilerId.F' of type sourcecode.fortran.f90 for architecture x86_64
Write auxiliary files
write-file /Users/Knowledge/Developer/Tutorials/MixingFortranAndC/XCode_CMake_FortranMixing/build/CMakeFiles/3.7.0/CompilerIdFortran/CompilerIdFortran.build/Debug/CompilerIdFortran.build/Script-2C8FEB8E15DC1A1A00E56A5D.sh
chmod 0755 /Users/Knowledge/Developer/Tutorials/MixingFortranAndC/XCode_CMake_FortranMixing/build/CMakeFiles/3.7.0/CompilerIdFortran/CompilerIdFortran.build/Debug/CompilerIdFortran.build/Script-2C8FEB8E15DC1A1A00E56A5D.sh
PhaseScriptExecution Run\ Script CompilerIdFortran.build/Debug/CompilerIdFortran.build/Script-2C8FEB8E15DC1A1A00E56A5D.sh
cd /Users/Knowledge/Developer/Tutorials/MixingFortranAndC/XCode_CMake_FortranMixing/build/CMakeFiles/3.7.0/CompilerIdFortran
/bin/sh -c /Users/Knowledge/Developer/Tutorials/MixingFortranAndC/XCode_CMake_FortranMixing/build/CMakeFiles/3.7.0/CompilerIdFortran/CompilerIdFortran.build/Debug/CompilerIdFortran.build/Script-2C8FEB8E15DC1A1A00E56A5D.sh
GCC_VERSION=com.apple.compilers.llvm.clang.1_0
** BUILD SUCCEEDED **
Compiling the Fortran compiler identification source file "CMakeFortranCompilerId.F" succeeded.
Compiler:
Build flags:
Id flags:
The output was:
0
=== BUILD TARGET CompilerIdFortran OF PROJECT CompilerIdFortran WITH THE DEFAULT CONFIGURATION (Debug) ===
Check dependencies
warning: no rule to process file '/Users/Knowledge/Developer/Tutorials/MixingFortranAndC/XCode_CMake_FortranMixing/build/CMakeFiles/3.7.0/CompilerIdFortran/CMakeFortranCompilerId.F' of type sourcecode.fortran.f90 for architecture x86_64
Write auxiliary files
write-file /Users/Knowledge/Developer/Tutorials/MixingFortranAndC/XCode_CMake_FortranMixing/build/CMakeFiles/3.7.0/CompilerIdFortran/CompilerIdFortran.build/Debug/CompilerIdFortran.build/Script-2C8FEB8E15DC1A1A00E56A5D.sh
chmod 0755 /Users/Knowledge/Developer/Tutorials/MixingFortranAndC/XCode_CMake_FortranMixing/build/CMakeFiles/3.7.0/CompilerIdFortran/CompilerIdFortran.build/Debug/CompilerIdFortran.build/Script-2C8FEB8E15DC1A1A00E56A5D.sh
PhaseScriptExecution Run\ Script CompilerIdFortran.build/Debug/CompilerIdFortran.build/Script-2C8FEB8E15DC1A1A00E56A5D.sh
cd /Users/Knowledge/Developer/Tutorials/MixingFortranAndC/XCode_CMake_FortranMixing/build/CMakeFiles/3.7.0/CompilerIdFortran
/bin/sh -c /Users/Knowledge/Developer/Tutorials/MixingFortranAndC/XCode_CMake_FortranMixing/build/CMakeFiles/3.7.0/CompilerIdFortran/CompilerIdFortran.build/Debug/CompilerIdFortran.build/Script-2C8FEB8E15DC1A1A00E56A5D.sh
GCC_VERSION=com.apple.compilers.llvm.clang.1_0
** BUILD SUCCEEDED **
Compiling the Fortran compiler identification source file "CMakeFortranCompilerId.F" succeeded.
Compiler: /opt/local/bin/gfortran
Build flags:
Id flags: -v
The output was:
0
Driving: /opt/local/bin/gfortran -v CMakeFortranCompilerId.F -mmacosx-version-min=10.12.1 -l gfortran -shared-libgcc
Using built-in specs.
COLLECT_GCC=/opt/local/bin/gfortran
COLLECT_LTO_WRAPPER=/opt/local/libexec/gcc/x86_64-apple-darwin16/6.2.0/lto-wrapper
Target: x86_64-apple-darwin16
Configured with: /opt/local/var/macports/build/_opt_bblocal_var_buildworker_ports_build_ports_lang_gcc6/gcc6/work/gcc-6.2.0/configure --prefix=/opt/local --build=x86_64-apple-darwin16 --enable-languages=c,c++,objc,obj-c++,lto,fortran --libdir=/opt/local/lib/gcc6 --includedir=/opt/local/include/gcc6 --infodir=/opt/local/share/info --mandir=/opt/local/share/man --datarootdir=/opt/local/share/gcc-6 --with-local-prefix=/opt/local --with-system-zlib --disable-nls --program-suffix=-mp-6 --with-gxx-include-dir=/opt/local/include/gcc6/c++/ --with-gmp=/opt/local --with-mpfr=/opt/local --with-mpc=/opt/local --with-isl=/opt/local --enable-stage1-checking --disable-multilib --enable-lto --enable-libstdcxx-time --with-build-config=bootstrap-debug --with-as=/opt/local/bin/as --with-ld=/opt/local/bin/ld --with-ar=/opt/local/bin/ar --with-bugurl=https://trac.macports.org/newticket --with-pkgversion='MacPorts gcc6 6.2.0_2'
Thread model: posix
gcc version 6.2.0 (MacPorts gcc6 6.2.0_2)
COLLECT_GCC_OPTIONS='-v' '-mmacosx-version-min=10.12.1' '-shared-libgcc' '-mtune=core2'
/opt/local/libexec/gcc/x86_64-apple-darwin16/6.2.0/f951 CMakeFortranCompilerId.F -ffixed-form -cpp=/var/folders/gn/07hkp3pd0r53c8sqglm6_3g40000gn/T//ccYnQZ27.f90 -quiet -v -D__DYNAMIC__ CMakeFortranCompilerId.F -fPIC -quiet -dumpbase CMakeFortranCompilerId.F -mmacosx-version-min=10.12.1 -mtune=core2 -auxbase CMakeFortranCompilerId -version -fintrinsic-modules-path /opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0/finclude -o /var/folders/gn/07hkp3pd0r53c8sqglm6_3g40000gn/T//ccfUnwPI.s
GNU Fortran (MacPorts gcc6 6.2.0_2) version 6.2.0 (x86_64-apple-darwin16)
compiled by GNU C version 6.2.0, GMP version 6.1.1, MPFR version 3.1.4, MPC version 1.0.3, isl version 0.14 or 0.13
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0/../../../../../x86_64-apple-darwin16/include"
#include "..." search starts here:
#include <...> search starts here:
/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0/finclude
/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0/include
/opt/local/include
/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0/include-fixed
/usr/include
End of search list.
GNU Fortran2008 (MacPorts gcc6 6.2.0_2) version 6.2.0 (x86_64-apple-darwin16)
compiled by GNU C version 6.2.0, GMP version 6.1.1, MPFR version 3.1.4, MPC version 1.0.3, isl version 0.14 or 0.13
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
COLLECT_GCC_OPTIONS='-v' '-mmacosx-version-min=10.12.1' '-shared-libgcc' '-mtune=core2'
/opt/local/bin/as -v -arch x86_64 -force_cpusubtype_ALL -o /var/folders/gn/07hkp3pd0r53c8sqglm6_3g40000gn/T//ccExsLaB.o /var/folders/gn/07hkp3pd0r53c8sqglm6_3g40000gn/T//ccfUnwPI.s
Apple Inc version cctools-886, GNU assembler version 1.38
Reading specs from /opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0/../../../libgfortran.spec
rename spec lib to liborig
COLLECT_GCC_OPTIONS='-v' '-mmacosx-version-min=10.12.1' '-shared-libgcc' '-mtune=core2'
COMPILER_PATH=/opt/local/libexec/gcc/x86_64-apple-darwin16/6.2.0/:/opt/local/libexec/gcc/x86_64-apple-darwin16/6.2.0/:/opt/local/libexec/gcc/x86_64-apple-darwin16/:/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0/:/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/
LIBRARY_PATH=/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0/:/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0/../../../
COLLECT_GCC_OPTIONS='-v' '-mmacosx-version-min=10.12.1' '-shared-libgcc' '-mtune=core2'
/opt/local/libexec/gcc/x86_64-apple-darwin16/6.2.0/collect2 -dynamic -arch x86_64 -macosx_version_min 10.12.1 -weak_reference_mismatches non-weak -o a.out -L/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0 -L/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0/../../.. /var/folders/gn/07hkp3pd0r53c8sqglm6_3g40000gn/T//ccExsLaB.o -lgfortran -no_compact_unwind -lSystem -lgcc_ext.10.5 -lgcc -lquadmath -lm -lgcc_ext.10.5 -lgcc -lSystem -v
collect2 version 6.2.0
/opt/local/bin/ld -dynamic -arch x86_64 -macosx_version_min 10.12.1 -weak_reference_mismatches non-weak -o a.out -L/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0 -L/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0/../../.. /var/folders/gn/07hkp3pd0r53c8sqglm6_3g40000gn/T//ccExsLaB.o -lgfortran -no_compact_unwind -lSystem -lgcc_ext.10.5 -lgcc -lquadmath -lm -lgcc_ext.10.5 -lgcc -lSystem -v
#(#)PROGRAM:ld PROJECT:ld64-264.3.102
configured to support archs: i386 x86_64 x86_64h armv6 armv7 armv7s armv7m armv7k arm64 (tvOS)
Library search paths:
/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0
/opt/local/lib/gcc6
/usr/lib
/usr/local/lib
Framework search paths:
/Library/Frameworks/
/System/Library/Frameworks/
/opt/local/bin/nm -n /var/folders/gn/07hkp3pd0r53c8sqglm6_3g40000gn/T//ccExsLaB.o
Compilation of the Fortran compiler identification source "CMakeFortranCompilerId.F" produced "a.out"
The Fortran compiler identification is GNU, found in "/Users/Knowledge/Developer/Tutorials/MixingFortranAndC/XCode_CMake_FortranMixing/build/CMakeFiles/3.7.0/CompilerIdFortran/a.out"
Determining if the Fortran compiler works passed with the following output:
Change Dir: /Users/Knowledge/Developer/Tutorials/MixingFortranAndC/XCode_CMake_FortranMixing/build/CMakeFiles/CMakeTmp
Run Build Command:"/usr/bin/make" "cmTC_ec5cc/fast"
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/cmTC_ec5cc.dir/build.make CMakeFiles/cmTC_ec5cc.dir/build
Building Fortran object CMakeFiles/cmTC_ec5cc.dir/testFortranCompiler.f.o
/opt/local/bin/gfortran -c /Users/Knowledge/Developer/Tutorials/MixingFortranAndC/XCode_CMake_FortranMixing/build/CMakeFiles/CMakeTmp/testFortranCompiler.f -o CMakeFiles/cmTC_ec5cc.dir/testFortranCompiler.f.o
Linking Fortran executable cmTC_ec5cc
/opt/local/bin/cmake -E cmake_link_script CMakeFiles/cmTC_ec5cc.dir/link.txt --verbose=1
/opt/local/bin/gfortran CMakeFiles/cmTC_ec5cc.dir/testFortranCompiler.f.o -o cmTC_ec5cc
Detecting Fortran compiler ABI info compiled with the following output:
Change Dir: /Users/Knowledge/Developer/Tutorials/MixingFortranAndC/XCode_CMake_FortranMixing/build/CMakeFiles/CMakeTmp
Run Build Command:"/usr/bin/make" "cmTC_61cbd/fast"
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/cmTC_61cbd.dir/build.make CMakeFiles/cmTC_61cbd.dir/build
Building Fortran object CMakeFiles/cmTC_61cbd.dir/CMakeFortranCompilerABI.F.o
/opt/local/bin/gfortran -c /opt/local/share/cmake-3.7/Modules/CMakeFortranCompilerABI.F -o CMakeFiles/cmTC_61cbd.dir/CMakeFortranCompilerABI.F.o
Linking Fortran executable cmTC_61cbd
/opt/local/bin/cmake -E cmake_link_script CMakeFiles/cmTC_61cbd.dir/link.txt --verbose=1
/opt/local/bin/gfortran -v -Wl,-v CMakeFiles/cmTC_61cbd.dir/CMakeFortranCompilerABI.F.o -o cmTC_61cbd
Driving: /opt/local/bin/gfortran -v -Wl,-v CMakeFiles/cmTC_61cbd.dir/CMakeFortranCompilerABI.F.o -o cmTC_61cbd -mmacosx-version-min=10.12.1 -l gfortran -shared-libgcc
Using built-in specs.
COLLECT_GCC=/opt/local/bin/gfortran
COLLECT_LTO_WRAPPER=/opt/local/libexec/gcc/x86_64-apple-darwin16/6.2.0/lto-wrapper
Target: x86_64-apple-darwin16
Configured with: /opt/local/var/macports/build/_opt_bblocal_var_buildworker_ports_build_ports_lang_gcc6/gcc6/work/gcc-6.2.0/configure --prefix=/opt/local --build=x86_64-apple-darwin16 --enable-languages=c,c++,objc,obj-c++,lto,fortran --libdir=/opt/local/lib/gcc6 --includedir=/opt/local/include/gcc6 --infodir=/opt/local/share/info --mandir=/opt/local/share/man --datarootdir=/opt/local/share/gcc-6 --with-local-prefix=/opt/local --with-system-zlib --disable-nls --program-suffix=-mp-6 --with-gxx-include-dir=/opt/local/include/gcc6/c++/ --with-gmp=/opt/local --with-mpfr=/opt/local --with-mpc=/opt/local --with-isl=/opt/local --enable-stage1-checking --disable-multilib --enable-lto --enable-libstdcxx-time --with-build-config=bootstrap-debug --with-as=/opt/local/bin/as --with-ld=/opt/local/bin/ld --with-ar=/opt/local/bin/ar --with-bugurl=https://trac.macports.org/newticket --with-pkgversion='MacPorts gcc6 6.2.0_2'
Thread model: posix
gcc version 6.2.0 (MacPorts gcc6 6.2.0_2)
Reading specs from /opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0/../../../libgfortran.spec
rename spec lib to liborig
COLLECT_GCC_OPTIONS='-v' '-o' 'cmTC_61cbd' '-mmacosx-version-min=10.12.1' '-shared-libgcc' '-mtune=core2'
COMPILER_PATH=/opt/local/libexec/gcc/x86_64-apple-darwin16/6.2.0/:/opt/local/libexec/gcc/x86_64-apple-darwin16/6.2.0/:/opt/local/libexec/gcc/x86_64-apple-darwin16/:/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0/:/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/
LIBRARY_PATH=/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0/:/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0/../../../
COLLECT_GCC_OPTIONS='-v' '-o' 'cmTC_61cbd' '-mmacosx-version-min=10.12.1' '-shared-libgcc' '-mtune=core2'
/opt/local/libexec/gcc/x86_64-apple-darwin16/6.2.0/collect2 -dynamic -arch x86_64 -macosx_version_min 10.12.1 -weak_reference_mismatches non-weak -o cmTC_61cbd -L/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0 -L/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0/../../.. -v CMakeFiles/cmTC_61cbd.dir/CMakeFortranCompilerABI.F.o -lgfortran -no_compact_unwind -lSystem -lgcc_ext.10.5 -lgcc -lquadmath -lm -lgcc_ext.10.5 -lgcc -lSystem -v
collect2 version 6.2.0
/opt/local/bin/ld -dynamic -arch x86_64 -macosx_version_min 10.12.1 -weak_reference_mismatches non-weak -o cmTC_61cbd -L/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0 -L/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0/../../.. -v CMakeFiles/cmTC_61cbd.dir/CMakeFortranCompilerABI.F.o -lgfortran -no_compact_unwind -lSystem -lgcc_ext.10.5 -lgcc -lquadmath -lm -lgcc_ext.10.5 -lgcc -lSystem -v
#(#)PROGRAM:ld PROJECT:ld64-264.3.102
configured to support archs: i386 x86_64 x86_64h armv6 armv7 armv7s armv7m armv7k arm64 (tvOS)
#(#)PROGRAM:ld PROJECT:ld64-264.3.102
configured to support archs: i386 x86_64 x86_64h armv6 armv7 armv7s armv7m armv7k arm64 (tvOS)
Library search paths:
/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0
/opt/local/lib/gcc6
/usr/lib
/usr/local/lib
Framework search paths:
/Library/Frameworks/
/System/Library/Frameworks/
/opt/local/bin/nm -n CMakeFiles/cmTC_61cbd.dir/CMakeFortranCompilerABI.F.o
Parsed Fortran implicit link information from above output:
link line regex: [^( *|.*[/\])(ld|([^/\]+-)?ld|collect2)[^/\]*( |$)]
ignore line: [Change Dir: /Users/Knowledge/Developer/Tutorials/MixingFortranAndC/XCode_CMake_FortranMixing/build/CMakeFiles/CMakeTmp]
ignore line: []
ignore line: [Run Build Command:"/usr/bin/make" "cmTC_61cbd/fast"]
ignore line: [/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/cmTC_61cbd.dir/build.make CMakeFiles/cmTC_61cbd.dir/build]
ignore line: [Building Fortran object CMakeFiles/cmTC_61cbd.dir/CMakeFortranCompilerABI.F.o]
ignore line: [/opt/local/bin/gfortran -c /opt/local/share/cmake-3.7/Modules/CMakeFortranCompilerABI.F -o CMakeFiles/cmTC_61cbd.dir/CMakeFortranCompilerABI.F.o]
ignore line: [Linking Fortran executable cmTC_61cbd]
ignore line: [/opt/local/bin/cmake -E cmake_link_script CMakeFiles/cmTC_61cbd.dir/link.txt --verbose=1]
ignore line: [/opt/local/bin/gfortran -v -Wl,-v CMakeFiles/cmTC_61cbd.dir/CMakeFortranCompilerABI.F.o -o cmTC_61cbd ]
ignore line: [Driving: /opt/local/bin/gfortran -v -Wl,-v CMakeFiles/cmTC_61cbd.dir/CMakeFortranCompilerABI.F.o -o cmTC_61cbd -mmacosx-version-min=10.12.1 -l gfortran -shared-libgcc]
ignore line: [Using built-in specs.]
ignore line: [COLLECT_GCC=/opt/local/bin/gfortran]
ignore line: [COLLECT_LTO_WRAPPER=/opt/local/libexec/gcc/x86_64-apple-darwin16/6.2.0/lto-wrapper]
ignore line: [Target: x86_64-apple-darwin16]
ignore line: [Configured with: /opt/local/var/macports/build/_opt_bblocal_var_buildworker_ports_build_ports_lang_gcc6/gcc6/work/gcc-6.2.0/configure --prefix=/opt/local --build=x86_64-apple-darwin16 --enable-languages=c,c++,objc,obj-c++,lto,fortran --libdir=/opt/local/lib/gcc6 --includedir=/opt/local/include/gcc6 --infodir=/opt/local/share/info --mandir=/opt/local/share/man --datarootdir=/opt/local/share/gcc-6 --with-local-prefix=/opt/local --with-system-zlib --disable-nls --program-suffix=-mp-6 --with-gxx-include-dir=/opt/local/include/gcc6/c++/ --with-gmp=/opt/local --with-mpfr=/opt/local --with-mpc=/opt/local --with-isl=/opt/local --enable-stage1-checking --disable-multilib --enable-lto --enable-libstdcxx-time --with-build-config=bootstrap-debug --with-as=/opt/local/bin/as --with-ld=/opt/local/bin/ld --with-ar=/opt/local/bin/ar --with-bugurl=https://trac.macports.org/newticket --with-pkgversion='MacPorts gcc6 6.2.0_2']
ignore line: [Thread model: posix]
ignore line: [gcc version 6.2.0 (MacPorts gcc6 6.2.0_2) ]
ignore line: [Reading specs from /opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0/../../../libgfortran.spec]
ignore line: [rename spec lib to liborig]
ignore line: [COLLECT_GCC_OPTIONS='-v' '-o' 'cmTC_61cbd' '-mmacosx-version-min=10.12.1' '-shared-libgcc' '-mtune=core2']
ignore line: [COMPILER_PATH=/opt/local/libexec/gcc/x86_64-apple-darwin16/6.2.0/:/opt/local/libexec/gcc/x86_64-apple-darwin16/6.2.0/:/opt/local/libexec/gcc/x86_64-apple-darwin16/:/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0/:/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/]
ignore line: [LIBRARY_PATH=/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0/:/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0/../../../]
ignore line: [COLLECT_GCC_OPTIONS='-v' '-o' 'cmTC_61cbd' '-mmacosx-version-min=10.12.1' '-shared-libgcc' '-mtune=core2']
link line: [ /opt/local/libexec/gcc/x86_64-apple-darwin16/6.2.0/collect2 -dynamic -arch x86_64 -macosx_version_min 10.12.1 -weak_reference_mismatches non-weak -o cmTC_61cbd -L/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0 -L/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0/../../.. -v CMakeFiles/cmTC_61cbd.dir/CMakeFortranCompilerABI.F.o -lgfortran -no_compact_unwind -lSystem -lgcc_ext.10.5 -lgcc -lquadmath -lm -lgcc_ext.10.5 -lgcc -lSystem -v]
arg [/opt/local/libexec/gcc/x86_64-apple-darwin16/6.2.0/collect2] ==> ignore
arg [-dynamic] ==> ignore
arg [-arch] ==> ignore
arg [x86_64] ==> ignore
arg [-macosx_version_min] ==> ignore
arg [10.12.1] ==> ignore
arg [-weak_reference_mismatches] ==> ignore
arg [non-weak] ==> ignore
arg [-o] ==> ignore
arg [cmTC_61cbd] ==> ignore
arg [-L/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0] ==> dir [/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0]
arg [-L/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0/../../..] ==> dir [/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0/../../..]
arg [-v] ==> ignore
arg [CMakeFiles/cmTC_61cbd.dir/CMakeFortranCompilerABI.F.o] ==> ignore
arg [-lgfortran] ==> lib [gfortran]
arg [-no_compact_unwind] ==> ignore
arg [-lSystem] ==> lib [System]
arg [-lgcc_ext.10.5] ==> lib [gcc_ext.10.5]
arg [-lgcc] ==> lib [gcc]
arg [-lquadmath] ==> lib [quadmath]
arg [-lm] ==> lib [m]
arg [-lgcc_ext.10.5] ==> lib [gcc_ext.10.5]
arg [-lgcc] ==> lib [gcc]
arg [-lSystem] ==> lib [System]
arg [-v] ==> ignore
Library search paths: [;/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0;/opt/local/lib/gcc6;/usr/lib;/usr/local/lib]
Framework search paths: [;/Library/Frameworks/;/System/Library/Frameworks/]
remove lib [System]
remove lib [gcc_ext.10.5]
remove lib [gcc]
remove lib [gcc_ext.10.5]
remove lib [gcc]
remove lib [System]
collapse library dir [/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0] ==> [/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0]
collapse library dir [/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0/../../..] ==> [/opt/local/lib/gcc6]
collapse library dir [/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0] ==> [/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0]
collapse library dir [/opt/local/lib/gcc6] ==> [/opt/local/lib/gcc6]
collapse library dir [/usr/lib] ==> [/usr/lib]
collapse library dir [/usr/local/lib] ==> [/usr/local/lib]
collapse framework dir [/Library/Frameworks/] ==> [/Library/Frameworks]
collapse framework dir [/System/Library/Frameworks/] ==> [/System/Library/Frameworks]
implicit libs: [gfortran;quadmath;m]
implicit dirs: [/opt/local/lib/gcc6/gcc/x86_64-apple-darwin16/6.2.0;/opt/local/lib/gcc6;/usr/lib;/usr/local/lib]
implicit fwks: [/Library/Frameworks;/System/Library/Frameworks]
Determining if the Fortran compiler supports Fortran 90 passed with the following output:
Change Dir: /Users/Knowledge/Developer/Tutorials/MixingFortranAndC/XCode_CMake_FortranMixing/build/CMakeFiles/CMakeTmp
Run Build Command:"/usr/bin/make" "cmTC_e62a9/fast"
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/cmTC_e62a9.dir/build.make CMakeFiles/cmTC_e62a9.dir/build
Building Fortran object CMakeFiles/cmTC_e62a9.dir/testFortranCompilerF90.f90.o
/opt/local/bin/gfortran -c /Users/Knowledge/Developer/Tutorials/MixingFortranAndC/XCode_CMake_FortranMixing/build/CMakeFiles/CMakeTmp/testFortranCompilerF90.f90 -o CMakeFiles/cmTC_e62a9.dir/testFortranCompilerF90.f90.o
Linking Fortran executable cmTC_e62a9
/opt/local/bin/cmake -E cmake_link_script CMakeFiles/cmTC_e62a9.dir/link.txt --verbose=1
/opt/local/bin/gfortran CMakeFiles/cmTC_e62a9.dir/testFortranCompilerF90.f90.o -o cmTC_e62a9
CMAKE stdout
-- The CXX compiler identification is AppleClang 8.0.0.8000042
-- The Fortran compiler identification is unknown
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at CMakeLists.txt:2 (project):
No CMAKE_Fortran_COMPILER could be found.

How to compile this simple C++ code using `openmp`

Here is the code
#include <omp.h>
#include <stdio.h>
int main() {
#pragma omp parallel
printf("Hello from thread %d, nthreads %d\n", omp_get_thread_num(), omp_get_num_threads());
}
which is copy-paste from #prakharjain answer at Installing OpenMP on Mac OS X 10.11.
Compiling with gcc-6 -fopenmp -o a a.cpp returns
Undefined symbols for architecture x86_64:
"___gxx_personality_v0", referenced from:
Dwarf Exception Unwind Info (__eh_frame) in ccyHyU08.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
Note that if I comment out #pragma omp parallel, then it compiles fine. I installed gcc-6 today following #AlejandroDanielNoel technic as explained at Installing OpenMP on Mac OS X 10.11. I am on Mac OS X 10.12.1. I have 4 logical cores on my machine.
Can you please help me compiling this simple code?
Here is the whole output when compiling with gcc-6 -v -fopenmp -o a a.cpp
Using built-in specs.
COLLECT_GCC=gcc-6
COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc/6.2.0/libexec/gcc/x86_64-apple-darwin16.1.0/6.2.0/lto-wrapper
Target: x86_64-apple-darwin16.1.0
Configured with: ../configure --build=x86_64-apple-darwin16.1.0 --prefix=/usr/local/Cellar/gcc/6.2.0 --libdir=/usr/local/Cellar/gcc/6.2.0/lib/gcc/6 --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-6 --with-gmp=/usr/local/opt/gmp --with-mpfr=/usr/local/opt/mpfr --with-mpc=/usr/local/opt/libmpc --with-isl=/usr/local/opt/isl --with-system-zlib --enable-libstdcxx-time=yes --enable-stage1-checking --enable-checking=release --enable-lto --with-build-config=bootstrap-debug --disable-werror --with-pkgversion='Homebrew gcc 6.2.0 --without-multilib' --with-bugurl=https://github.com/Homebrew/homebrew/issues --enable-plugin --disable-nls --disable-multilib --with-native-system-header-dir=/usr/include --with-sysroot=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk
Thread model: posix
gcc version 6.2.0 (Homebrew gcc 6.2.0 --without-multilib)
COLLECT_GCC_OPTIONS='-v' '-fopenmp' '-o' 'a' '-mmacosx-version-min=10.12.1' '-mtune=core2'
/usr/local/Cellar/gcc/6.2.0/libexec/gcc/x86_64-apple-darwin16.1.0/6.2.0/cc1plus -quiet -v -D__DYNAMIC__ a.cpp -fPIC -quiet -dumpbase a.cpp -mmacosx-version-min=10.12.1 -mtune=core2 -auxbase a -version -fopenmp -o /var/folders/vf/5w2jgk0d0hx582mm92nrk8zm0000gn/T//ccZjzRvK.s
GNU C++14 (Homebrew gcc 6.2.0 --without-multilib) version 6.2.0 (x86_64-apple-darwin16.1.0)
compiled by GNU C version 6.2.0, GMP version 6.1.1, MPFR version 3.1.5, MPC version 1.0.3, isl version 0.15
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/local/include"
ignoring nonexistent directory "/usr/local/Cellar/gcc/6.2.0/lib/gcc/6/gcc/x86_64-apple-darwin16.1.0/6.2.0/../../../../../../x86_64-apple-darwin16.1.0/include"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/Cellar/gcc/6.2.0/lib/gcc/6/gcc/x86_64-apple-darwin16.1.0/6.2.0/../../../../../../include/c++/6.2.0
/usr/local/Cellar/gcc/6.2.0/lib/gcc/6/gcc/x86_64-apple-darwin16.1.0/6.2.0/../../../../../../include/c++/6.2.0/x86_64-apple-darwin16.1.0
/usr/local/Cellar/gcc/6.2.0/lib/gcc/6/gcc/x86_64-apple-darwin16.1.0/6.2.0/../../../../../../include/c++/6.2.0/backward
/usr/local/Cellar/gcc/6.2.0/lib/gcc/6/gcc/x86_64-apple-darwin16.1.0/6.2.0/include
/usr/local/Cellar/gcc/6.2.0/lib/gcc/6/gcc/x86_64-apple-darwin16.1.0/6.2.0/include-fixed
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/System/Library/Frameworks
End of search list.
GNU C++14 (Homebrew gcc 6.2.0 --without-multilib) version 6.2.0 (x86_64-apple-darwin16.1.0)
compiled by GNU C version 6.2.0, GMP version 6.1.1, MPFR version 3.1.5, MPC version 1.0.3, isl version 0.15
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 20fdc54fe5944e0048cbe59ee8edcdd5
COLLECT_GCC_OPTIONS='-v' '-fopenmp' '-o' 'a' '-mmacosx-version-min=10.12.1' '-mtune=core2'
as -arch x86_64 -force_cpusubtype_ALL -o /var/folders/vf/5w2jgk0d0hx582mm92nrk8zm0000gn/T//ccH3Ixr9.o /var/folders/vf/5w2jgk0d0hx582mm92nrk8zm0000gn/T//ccZjzRvK.s
COMPILER_PATH=/usr/local/Cellar/gcc/6.2.0/libexec/gcc/x86_64-apple-darwin16.1.0/6.2.0/:/usr/local/Cellar/gcc/6.2.0/libexec/gcc/x86_64-apple-darwin16.1.0/6.2.0/:/usr/local/Cellar/gcc/6.2.0/libexec/gcc/x86_64-apple-darwin16.1.0/:/usr/local/Cellar/gcc/6.2.0/lib/gcc/6/gcc/x86_64-apple-darwin16.1.0/6.2.0/:/usr/local/Cellar/gcc/6.2.0/lib/gcc/6/gcc/x86_64-apple-darwin16.1.0/
LIBRARY_PATH=/usr/local/Cellar/gcc/6.2.0/lib/gcc/6/gcc/x86_64-apple-darwin16.1.0/6.2.0/:/usr/local/Cellar/gcc/6.2.0/lib/gcc/6/gcc/x86_64-apple-darwin16.1.0/6.2.0/../../../
COLLECT_GCC_OPTIONS='-v' '-fopenmp' '-o' 'a' '-mmacosx-version-min=10.12.1' '-mtune=core2'
/usr/local/Cellar/gcc/6.2.0/libexec/gcc/x86_64-apple-darwin16.1.0/6.2.0/collect2 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/ -dynamic -arch x86_64 -macosx_version_min 10.12.1 -weak_reference_mismatches non-weak -o a -L/usr/local/Cellar/gcc/6.2.0/lib/gcc/6/gcc/x86_64-apple-darwin16.1.0/6.2.0 -L/usr/local/Cellar/gcc/6.2.0/lib/gcc/6/gcc/x86_64-apple-darwin16.1.0/6.2.0/../../.. /var/folders/vf/5w2jgk0d0hx582mm92nrk8zm0000gn/T//ccH3Ixr9.o -lgomp -no_compact_unwind -lSystem -lgcc_ext.10.5 -lgcc -lSystem -v
collect2 version 6.2.0
/usr/bin/ld -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/ -dynamic -arch x86_64 -macosx_version_min 10.12.1 -weak_reference_mismatches non-weak -o a -L/usr/local/Cellar/gcc/6.2.0/lib/gcc/6/gcc/x86_64-apple-darwin16.1.0/6.2.0 -L/usr/local/Cellar/gcc/6.2.0/lib/gcc/6/gcc/x86_64-apple-darwin16.1.0/6.2.0/../../.. /var/folders/vf/5w2jgk0d0hx582mm92nrk8zm0000gn/T//ccH3Ixr9.o -lgomp -no_compact_unwind -lSystem -lgcc_ext.10.5 -lgcc -lSystem -v
#(#)PROGRAM:ld PROJECT:ld64-274.1
configured to support archs: armv6 armv7 armv7s arm64 i386 x86_64 x86_64h armv6m armv7k armv7m armv7em (tvOS)
Library search paths:
/usr/local/Cellar/gcc/6.2.0/lib/gcc/6/gcc/x86_64-apple-darwin16.1.0/6.2.0
/usr/local/Cellar/gcc/6.2.0/lib/gcc/6
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/lib
Framework search paths:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/System/Library/Frameworks/
Undefined symbols for architecture x86_64:
"___gxx_personality_v0", referenced from:
Dwarf Exception Unwind Info (__eh_frame) in ccH3Ixr9.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
Just try g++-6 instead of gcc-6.

GCC: list a statically linked libraries

That's the question. Basically my problem is that I am trying to compile an app for ARM with an options -fsanitize=address -static-libasan (cool thing for debugging a wrong pointers and etc.). This compiles fine directly to executable file, but doesn't work for compiling from an object files. Ofc I found that the static lib is libasan.a; anyway when I am trying to link everything from an object file, it says about a bunch of an undefined references in «pthread» and some functions whose lib-owner I didn't even managed to find.
So, I just want to compile a file with -fsanitize=address -static-libasan and see which a libraries were linked statically, so that I could later do it from an object file.
An executable stores no trace of any static libraries it pulled object files from. Mostly these libraries are explicitly specified when linking, though. The compiler only pulls in a few standard static libraries. For gcc you can see various information including the actual command line passed to the linker using the -v option (it seems some implementations want to use --verbose and for the linker -Wl,--verbose). This output should show all implicitly linked libraries like -lgcc and -lstdc++.
Note that you should link using the language specific frontend and the same compiler flags as when compiling. Your problem description sounds as if you use -pthread with g++ when compiling but you tried to link using gcc without the -pthread flags (there are a few different threading related flags; from the description it is impossible to tell which one was used).
Just to deal with the insults, here is an example output when using the -v option on my computer:
ThreadGard:stackoverflow kuehl$ cat hello.cpp
#include <iostream>
int main()
{
std::cout << "hello, world\n";
return 0;
}
ThreadGard:stackoverflow kuehl$ g++ -v -static hello.cpp
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/opt/gcc-4.9.2/libexec/gcc/x86_64-apple-darwin13.4.0/4.9.2/lto-wrapper
Target: x86_64-apple-darwin13.4.0
Configured with: ../gcc-4.9.2/configure --prefix=/opt/gcc-4.9.2 --with-gmp=/opt/gcc-infrastructure --with-mpfr=/opt/gcc-infrastructure --enable-decimal-float=bid --enable-lto --enable-languages=c,c++
Thread model: posix
gcc version 4.9.2 (GCC)
COLLECT_GCC_OPTIONS='-mmacosx-version-min=10.9.4' '-v' '-static' '-mtune=core2'
/opt/gcc-4.9.2/libexec/gcc/x86_64-apple-darwin13.4.0/4.9.2/cc1plus -quiet -v -D__STATIC__ hello.cpp -quiet -dumpbase hello.cpp -mmacosx-version-min=10.9.4 -mtune=core2 -auxbase hello -version -o /var/folders/b_/64plfvs936v5ylx36qwc8rg00000gp/T//ccrt7fXS.s
GNU C++ (GCC) version 4.9.2 (x86_64-apple-darwin13.4.0)
compiled by GNU C version 4.9.2, GMP version 5.0.5, MPFR version 3.1.1, MPC version 1.0.1
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/4.9.2/../../../../x86_64-apple-darwin13.4.0/include"
#include "..." search starts here:
#include <...> search starts here:
/opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/4.9.2/../../../../include/c++/4.9.2
/opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/4.9.2/../../../../include/c++/4.9.2/x86_64-apple-darwin13.4.0
/opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/4.9.2/../../../../include/c++/4.9.2/backward
/opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/4.9.2/include
/usr/local/include
/opt/gcc-4.9.2/include
/opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/4.9.2/include-fixed
/usr/include
/System/Library/Frameworks
/Library/Frameworks
End of search list.
GNU C++ (GCC) version 4.9.2 (x86_64-apple-darwin13.4.0)
compiled by GNU C version 4.9.2, GMP version 5.0.5, MPFR version 3.1.1, MPC version 1.0.1
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: fea0def5b69cf8ae349b2f1faf4b1d23
COLLECT_GCC_OPTIONS='-mmacosx-version-min=10.9.4' '-v' '-static' '-mtune=core2'
as -arch x86_64 -force_cpusubtype_ALL -static -o /var/folders/b_/64plfvs936v5ylx36qwc8rg00000gp/T//ccPF77oR.o /var/folders/b_/64plfvs936v5ylx36qwc8rg00000gp/T//ccrt7fXS.s
COMPILER_PATH=/opt/gcc-4.9.2/libexec/gcc/x86_64-apple-darwin13.4.0/4.9.2/:/opt/gcc-4.9.2/libexec/gcc/x86_64-apple-darwin13.4.0/4.9.2/:/opt/gcc-4.9.2/libexec/gcc/x86_64-apple-darwin13.4.0/:/opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/4.9.2/:/opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/
LIBRARY_PATH=/opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/4.9.2/:/opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/4.9.2/../../../:/usr/lib/
COLLECT_GCC_OPTIONS='-mmacosx-version-min=10.9.4' '-v' '-static' '-mtune=core2'
/opt/gcc-4.9.2/libexec/gcc/x86_64-apple-darwin13.4.0/4.9.2/collect2 -static -arch x86_64 -macosx_version_min 10.9.4 -weak_reference_mismatches non-weak -o a.out -lcrt0.o -L/opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/4.9.2 -L/opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/4.9.2/../../.. /var/folders/b_/64plfvs936v5ylx36qwc8rg00000gp/T//ccPF77oR.o /opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/4.9.2/../../../libstdc++.a -no_compact_unwind -no_pie -lgcc_eh -lgcc -v
collect2 version 4.9.2
/usr/bin/ld -static -arch x86_64 -macosx_version_min 10.9.4 -weak_reference_mismatches non-weak -o a.out -lcrt0.o -L/opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/4.9.2 -L/opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/4.9.2/../../.. /var/folders/b_/64plfvs936v5ylx36qwc8rg00000gp/T//ccPF77oR.o /opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/4.9.2/../../../libstdc++.a -no_compact_unwind -no_pie -lgcc_eh -lgcc -v
#(#)PROGRAM:ld PROJECT:ld64-241.9
configured to support archs: armv6 armv7 armv7s arm64 i386 x86_64 x86_64h armv6m armv7m armv7em
Library search paths:
/opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/4.9.2
/opt/gcc-4.9.2/lib
/usr/lib
/usr/local/lib
Framework search paths:
/Library/Frameworks/
/System/Library/Frameworks/
ld: library not found for -lcrt0.o
collect2: error: ld returned 1 exit status
There are not all static libraries installed to make it actually link. However, that's besides the point as the output clearly lists the linker invocation:
/usr/bin/ld -static -arch x86_64 -macosx_version_min 10.9.4 -weak_reference_mismatches non-weak -o a.out -lcrt0.o -L/opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/4.9.2 -L/opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/4.9.2/../../.. /var/folders/b_/64plfvs936v5ylx36qwc8rg00000gp/T//ccPF77oR.o /opt/gcc-4.9.2/lib/gcc/x86_64-apple-darwin13.4.0/4.9.2/../../../libstdc++.a -no_compact_unwind -no_pie -lgcc_eh -lgcc -v
(with some emphasis added to make it easier for readers - it seems to be necessary)

What library does ld option -lrt refer to (Bionic libc)?

What does this option to ld mean?
-lrt
I know to some degree it means that ld is looking for the library with real-time extensions, but after searching extensively, I couldn't find an exact definition (or which library) this is referring to.
With Bionic (android) libc, the android NDK docs detail that this (along with pthreads) has been integrated.
I ask because it seems to be implicit (ie when I invoke the compiler, a custom-compiled gdc, directly, I get a message saying that ld cannot find -lrt)
EDIT
From the Bionic Overview
Bionic's C library comes with its own pthread implementation bundled
in. This is different from other historical C libraries which:
- place it in an external library (-lpthread)
- play linker tricks with weak symbols at dynamic link time
The support for real-time features (a.k.a. -lrt) is also bundled in
the C library.
The problem is that collect2 is passing the option -lrt (and also -lpthreads) to ld.
dan#devbox:~/projects/gdc_test/jni/src$ $DC gdc_test.d -v -mthumb
Using built-in specs.
Target: arm-linux-androideabi
Configured with: /arm/ndk-git/src/build/../gcc/gcc-4.4.3/configure --prefix=/arm/ndk-git/ndk/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86 --target=arm-linux-androideabi --host=x86_64-linux-gnu --build=x86_64-linux-gnu --with-gnu-as --with-gnu-ld --enable-languages=c,c++ --with-gmp=/arm/ndk-git/build_run/temp-install --with-mpfr=/arm/ndk-git/build_run/temp-install --disable-libssp --enable-threads --disable-nls --disable-libmudflap --disable-libgomp --disable-libstdc__-v3 --disable-sjlj-exceptions --disable-shared --disable-tls --enable-languages=c,d,c++ --enable-lto --with-float=soft --with-fpu=vfp --with-arch=armv5te --enable-target-optspace --enable-shared--prefix=/arm/ndk-git/ndk/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86 --enable-initfini-array --disable-nls --prefix=/arm/ndk-git/ndk/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86 --with-sysroot=/arm/ndk-git/ndk/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/sysroot --with-binutils-version=2.19 --with-mpfr-version=2.4.1 --with-gmp-version=4.2.4 --with-gcc-version=4.4.3 --with-gdb-version=6.6 --with-arch=armv5te --enable-libstdc__-v3 --disable-werrors --enable-languages=c,d,c++ --enable-shared --enable-multilib --disable-threads --program-transform-name='s,^,arm-linux-androideabi-,'
Thread model: single
gcc version 4.4.3 20100121 (gdc hg, using dmd 1.067) (GCC)
COLLECT_GCC_OPTIONS='-v' '-mthumb' '-shared-libgcc' '-pthread' '-march=armv5te' '-mfloat-abi=soft' '-mfpu=vfp'
/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../libexec/gcc/arm-linux-androideabi/4.4.3/cc1d gdc_test.d -mbionic -fpic -quiet -dumpbase gdc_test.d -mthumb -march=armv5te -mfloat-abi=soft -mfpu=vfp -auxbase gdc_test -version -imultilib thumb -iprefix /home/dan/projects/arm-linux-androideabi-4.4.3/bin/../lib/gcc/arm-linux-androideabi/4.4.3/ -isysroot /home/dan/projects/arm-linux-androideabi-4.4.3/bin/../sysroot -fmultilib-dir=thumb -o /tmp/cc2yu97y.s
GNU D (GCC) version 4.4.3 20100121 (gdc hg, using dmd 1.067) (arm-linux-androideabi)
compiled by GNU C version 4.5.2, GMP version 4.2.4, MPFR version 2.4.1.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU D (GCC) version 4.4.3 20100121 (gdc hg, using dmd 1.067) (arm-linux-androideabi)
compiled by GNU C version 4.5.2, GMP version 4.2.4, MPFR version 2.4.1.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
COLLECT_GCC_OPTIONS='-v' '-mthumb' '-shared-libgcc' '-pthread' '-march=armv5te' '-mfloat-abi=soft' '-mfpu=vfp'
/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/bin/as -v -march=armv5te -mfloat-abi=soft -mfpu=vfp -meabi=5 -o /tmp/ccuFGEfj.o /tmp/cc2yu97y.s
GNU assembler version 2.19 (arm-linux-androideabi) using BFD version (GNU Binutils) 2.19
COMPILER_PATH=/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../libexec/gcc/arm-linux-androideabi/4.4.3/:/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../libexec/gcc/:/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/bin/
LIBRARY_PATH=/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../lib/gcc/arm-linux-androideabi/4.4.3/thumb/:/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/lib/thumb/:/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../lib/gcc/arm-linux-androideabi/4.4.3/:/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../lib/gcc/:/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/lib/:/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../sysroot/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-mthumb' '-shared-libgcc' '-pthread' '-march=armv5te' '-mfloat-abi=soft' '-mfpu=vfp'
/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../libexec/gcc/arm-linux-androideabi/4.4.3/collect2 --sysroot=/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../sysroot --eh-frame-hdr -dynamic-linker /system/bin/linker -X -m armelf_linux_eabi /home/dan/projects/arm-linux-androideabi-4.4.3/bin/../sysroot/usr/lib/crtbegin_dynamic.o -L/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../lib/gcc/arm-linux-androideabi/4.4.3/thumb -L/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/lib/thumb -L/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../lib/gcc/arm-linux-androideabi/4.4.3 -L/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../lib/gcc -L/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/lib -L/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../sysroot/usr/lib /tmp/ccuFGEfj.o -lgphobos -lm -lrt -lgcc_s -lgcc -lpthread -lc -ldl -lgcc_s -lgcc /home/dan/projects/arm-linux-androideabi-4.4.3/bin/../sysroot/usr/lib/crtend_android.o
/home/dan/projects/arm-linux-androideabi-4.4.3/bin/../lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/bin/ld: cannot find -lrt
collect2: ld returned 1 exit status
Is there a way to override this behavior?
When you see a -lsomething, the library name is libsomething. Doing a man librt says the following:
NAME
librt, libposix4 - POSIX.1b Realtime Extensions library
SYNOPSIS
cc [ flag... ] file... -lrt [ library... ]
DESCRIPTION
Functions in this library provide most of the interfaces
specified by the POSIX.1b Realtime Extension. See stan-
dards(5). Specifically, this includes the interfaces defined
under the Asynchronous I/O, Message Passing, Process
Scheduling, Realtime Signals Extension, Semaphores, Shared
Memory Objects, Synchronized I/O, and Timers options. The
interfaces defined under the Memory Mapped Files, Process
Memory Locking, and Range Memory Locking options are pro-
vided in libc(3LIB)
See the man pages for the individual interfaces in section
3RT for information on required headers.
The name libposix4 is maintained for backward compatibility
and should be avoided. librt is the preferred name for this
library.
-l is the flag used to link a library into your final executable. In this case, the library in question is librt.a (or librt.so or librt.dylib, depending on your system). librt is the realtime extensions library.
Answering my own question: it was hardcoded into gdc/d/d-spec.c