[CMake] Are CMAKE_CXX_FLAGS supposed to go on the link line?
Paul Smith
paul at mad-scientist.net
Thu Jan 22 11:11:20 EST 2015
I didn't get a response to the question below. I've since reproduced
this with a simple test case; maybe someone can let me know why this
difference appears and how I should handle it? I'm using cmake 3.1.0:
Sample CMakeLists.txt:
$ cat >CMakeLists.txt <<EOF
cmake_minimum_required(VERSION 3.1.0)
project(FlagTest)
set(CMAKE_CXX_FLAGS -pthread)
add_executable(foo foo.cpp)
EOF
$ echo 'int main(int argc, char**argv) { return 0; }' > foo.cpp
On MacOSX with Xcode installed, I see the following behavior:
$ cmake -G Xcode . && cmake --build . 2>&1 | tee xc.out
In the output I see that the "-pthread" flag DOES appear on the compile
line for "foo.o", but DOES NOT appear in the link line for "foo":
$ grep pthread xc.out
/.../clang -x c++ ... -pthread ... -c /.../foo.cpp -o /.../foo.o
This is correct for clang, which does not want -pthread on the link line
(unlike GCC, which wants it in both places). Now, I clean that up and
try with the Makefile generator, and I see the following behavior:
$ cmake -G 'Unix Makefiles' . && cmake --build . -- VERBOSE=1 2>&1 | tee mk.out
clang: warning: argument unused during compilation: '-pthread'
Now in this case we can see that the -pthread flag was added to BOTH the
compile and the link line:
$ grep pthread mk.out
/usr/bin/c++ -pthread -o .../foo.cpp.o -c .../foo.cpp
/usr/bin/c++ -pthread .../foo.cpp.o -o foo
clang: warning: argument unused during compilation: '-pthread'
This warning is totally bogus: it really means "unused during LINKING",
but anyway: I'm not sure what to do to get rid of this warning: I need
to support both Xcode and Makefiles on MacOSX (and Makefiles on Linux as
well as both GCC and Clang).
Is the intent that CMAKE_CXX_FLAGS only ever appear on the compile line?
Or should it appear on both the compile and link lines? Is there an
equivalent flag to CMAKE_EXE_LINKER_FLAGS that always applies only to
compile lines, in all generators? Do we just have to continue to
bastardize add_definitions() for this, maybe?
On Thu, 2015-01-08 at 18:52 -0500, Paul Smith wrote:
> If I'm on OSX, then when I set CMAKE_CXX_FLAGS and do not set
> CMAKE_EXE_LINKER_FLAGS, for example, and I use the Xcode generator,
> then
> I see:
> * CMAKE_CXX_FLAGS show up on the compilation line
> * CMAKE_CXX_FLAGS do NOT show up in the linker line
>
> On the other hand if I'm on OSX and I use the Unix Makefiles
> generator,
> then I see:
> * CMAKE_CXX_FLAGS show up on the compilation line
> * CMAKE_CXX_FLAGS ALSO show up in the linker line
>
> I assume the Xcode output is correct and the makefile output (with
> CMAKE_CXX_FLAGS in both) is not correct... but there's nothing I can
> find in the docs that says this explicitly.
>
> I've printed the contents of the CMAKE_CXX_FLAGS and
> CMAKE_EXE_LINKER_FLAGS at the end of my CMakeLists.txt and they're
> just
> what I expect. It's just that the link line has extra flags, when
> invoked from make.
More information about the CMake
mailing list