The doc says: CMAKE_EXE_LINKER_FLAGS
Linker flags to be used to create executables. These flags will be used by the linker when creating an executable.
When I use -DCMAKE_EXE_LINKER_FLAGS="-L/opt/somelib/lib -Wl,--as-needed,-lsomelib,--no-as-needed"
I get the linker line:
case A. gcc -L/opt/somelib/lib -Wl,--as-needed,-lsomelib,--no-as-needed myfile.c.o -o executable
And this does not link, producing undefined symbol errors.
If instead I use:
case B. gcc myfile.c.o -L/opt/somelib/lib -Wl,--as-needed,-lsomelib,--no-as-needed -o executable
or
case C. gcc -L/opt/somelib/lib -lsomelib myfile.c.o -o executable
It links correctly.
This causes issues because AFAIK the gcc (driver of the linker) is not allowed to reorder .o .a .so files before/after a -Wl flags. In the C case, gcc reorders and the linker, which always looks right when seeking to satisfy undefined symbols can work to completion.
In the A case, gcc can not reorder the object file before -lsomelib and the linker fails.
Brad King told me that I should not be using CMAKE_EXE_LINKER_FLAGS
to add libraries as this variable is only for flags at the beginning of the linker command.
Is there a CMAKE_EXE_LINKER_FLAGS equivalent for telling CMake to put the flags after the object files ?