1

We are catching link errors on Solaris with makefiles generated by CMake 3.6.2. In the testing below, we are using GCC and not SunCC. From the looks of it, CMake is applying our options inconsistently:

Typical compile command

[  2%] Building CXX object CMakeFiles/cryptopp-object.dir/cpu.cpp.o
/bin/c++ -fPIC -march=native -m64 -Wa,--divide -o CMakeFiles/cryptopp-object.dir/cryptlib.cpp.o 
-c /export/home/jwalton/cryptopp/cpu.cpp

Abbreviated link command

/bin/c++ CMakeFiles/cryptest.dir/bench1.cpp.o CMakeFiles/cryptest.dir/bench2.cpp.o
...
CMakeFiles/cryptest.dir/fipstest.cpp.o  -o cryptest.exe  libcryptopp.a -lnsl -lsocket

Typical link error

ld: fatal: file CMakeFiles/cryptopp-object.dir/cryptlib.cpp.o: wrong ELF class: ELFCLASS64

Notice the file was compiled with -march=native -m64 (its a 64-bit capable machine and kernel), but the link invocation is missing it (the default is 32-bit on Solaris).

Attempting to search for "cmake use CXXFLAGS link" is producing too much irrelevant noise, and I'm not having much luck finding the CMakeList.txt option. I also want to avoid duplicating the work into LDFLAGS, or performing the work of reformatting the options (CXXFLAGS option -Wl,-x becomes LDFLAGS option -x).

How do I instruct CMake to use both CXX and CXXFLAGS when driving link?


I found Running a different program for the linker on the CMake users mailing list, but it does not feel right to me (also, the problem and context are slightly different). It also does not work.

Here is a small example:

PROJECT(foo)

SET(CMAKE_CXX_LINK_EXECUTABLE
    "purify <CMAKE_CXX_COMPILER> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> <FLAGS> <OBJECTS>  -o <TARGET> <LINK_LIBRARIES>")
ADD_EXECUTABLE(foo foo.cxx)

I also found Setting global link flags on the mailing list. It does not work, either.

SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_CXX_FLAGS}")
SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_CXX_FLAGS}")
SET(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_CXX_FLAGS}")
usr1234567
  • 21,601
  • 16
  • 108
  • 128
jww
  • 97,681
  • 90
  • 411
  • 885
  • 1
    Does CMake use `LDFLAGS`? Or see this: http://stackoverflow.com/questions/6077414/cmake-how-to-set-the-ldflags-in-cmakelists-txt – Andrew Henle Sep 14 '16 at 22:47
  • @AndrewHenle - According to some Camke user posts, it appears so. However, I'm not a guy who should b answering questions like that since I'm too inexperienced with Cmake. – jww Sep 14 '16 at 23:33
  • Probably related. In general, I wouldn't try to add CXXFLAGS to the linker. Just pass, what you actually need (`-x`) to the CMAKE_EXE_LINKER_FLAGS. Check with `make VERBOSE=1` whether the added flags appear or not. – usr1234567 Sep 15 '16 at 08:31
  • 1
    @usr1234567 - Forgive my ignorance... What does CMake do with Sanitizer builds? The sanitizer flags are supposed to be passed through the compiler driver, and the driver is supposed to pass the flag along with the appropriate libraries to the linker. We are explicitly told *not* to pass sanitizer libs to the linker; the compiler driver should handle it. – jww Sep 15 '16 at 08:38

0 Answers0