[CMake] Are CMAKE_CXX_FLAGS supposed to go on the link line?

Clinton Stimpson clinton at elemtech.com
Mon Mar 2 11:45:04 EST 2015


FYI,

I also came across this discrepancy recently, and logged a bug for it.
http://www.cmake.org/Bug/view.php?id=15427

With the example provided in the bug report, I am seeing link errors under 
Xcode which I do not see under Makefiles.

Clint 

On Monday, March 02, 2015 07:33:38 PM Roman Bolshakov wrote:
> Apparently, command line to link executable is quite different from a
> shared library (Modules/CMakeCXXInformation.cmake):
> 
> if(NOT CMAKE_CXX_LINK_EXECUTABLE)
>   set(CMAKE_CXX_LINK_EXECUTABLE
>     "<CMAKE_CXX_COMPILER>  <FLAGS> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS>
> <OBJECTS>  -o <TARGET> <LINK_LIBRARIES>")
> endif()
> 
> 
>   set(CMAKE_CXX_CREATE_SHARED_LIBRARY
>       "<CMAKE_CXX_COMPILER> <CMAKE_SHARED_LIBRARY_CXX_FLAGS>
> <LANGUAGE_COMPILE_FLAGS> <LINK_FLAGS>
> <CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS> <SONAME_FLAG><TARGET_SONAME>
> -o <TARGET> <OBJECTS> <LINK_LIBRARIES>")
> 
> CMAKE_CXX_FLAGS/CMAKE_C_FLAGS which are filled into
> <LANGUAGE_COMPILE_FLAGS> don't go into a linker command for
> executable.
> 
> 2015-01-22 19:11 GMT+03:00 Paul Smith <paul at mad-scientist.net>:
> > 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.
> > 
> > --
> > 
> > Powered by www.kitware.com
> > 
> > Please keep messages on-topic and check the CMake FAQ at:
> > http://www.cmake.org/Wiki/CMake_FAQ
> > 
> > Kitware offers various services to support the CMake community. For more
> > information on each offering, please visit:
> > 
> > CMake Support: http://cmake.org/cmake/help/support.html
> > CMake Consulting: http://cmake.org/cmake/help/consulting.html
> > CMake Training Courses: http://cmake.org/cmake/help/training.html
> > 
> > Visit other Kitware open-source projects at
> > http://www.kitware.com/opensource/opensource.html
> > 
> > Follow this link to subscribe/unsubscribe:
> > http://public.kitware.com/mailman/listinfo/cmake



More information about the CMake mailing list