When using clang++, how can I make CMake use the -std=c++0x flag when compiling, but not when linking?
There are several other posts regarding using clang as compiler, but I have not found any hints on setting the c++ standard.
Here is what I tried:
CMakeLists.txt:
project(test)
add_executable(main main.cxx)
ClangOverride.txt:
SET (CMAKE_C_FLAGS_INIT "-Wall -std=c99")
SET (CMAKE_C_FLAGS_DEBUG_INIT "-g")
SET (CMAKE_C_FLAGS_MINSIZEREL_INIT "-Os -DNDEBUG")
SET (CMAKE_C_FLAGS_RELEASE_INIT "-O3 -DNDEBUG")
SET (CMAKE_C_FLAGS_RELWITHDEBINFO_INIT "-O2 -g")
SET (CMAKE_CXX_FLAGS_INIT "-Wall -std=c++0x -stdlib=libc++")
SET (CMAKE_CXX_FLAGS_DEBUG_INIT "-g")
SET (CMAKE_CXX_FLAGS_MINSIZEREL_INIT "-Os -DNDEBUG")
SET (CMAKE_CXX_FLAGS_RELEASE_INIT "-O3 -DNDEBUG")
SET (CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT "-O2 -g")
SET (CMAKE_EXE_LINKER_FLAGS_INIT "")
main.cxx:
int main(){ return 0; }
Command used to invoke cmake
CC=clang CXX=clang++ cmake .. -DCMAKE_USER_MAKE_RULES_OVERRIDE=ClangOverride.txt -DCMAKE_BUILD_TYPE=Release
Building the project:
VERBOSE=1 make
This will invoke the following two commands:
/usr/bin/clang++ -Wall -std=c++0x -stdlib=libc++ -O3 -DNDEBUG -o CMakeFiles/main.dir/main.cxx.o -c /tmp/asdf/main.cxx
/usr/bin/clang++ -Wall -std=c++0x -stdlib=libc++ -O3 -DNDEBUG CMakeFiles/main.dir/main.cxx.o -o main -rdynamic
The second command results in warning because if the un-used flag: -std=c++0x
clang: warning: argument unused during compilation: '-std=c++0x'
CMakeFiles/main.dir/main.cxx.o: file not recognized: File format not recognized
clang: error: linker command failed with exit code 1 (use -v to see invocation)
How can I avoid this?
Use ADD_DEFINITIONS("-std=c++0x") instead of setting CXX flags.
Related
I tried to use -rdynamic option in my CMakeLists.txt file, like this:
cmake_minimum_required(VERSION 3.5)
project(Tmuduo CXX)
...
set(CMAKE_CXX_STANDARD 11)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
add_compile_options(-Wthread-safety )
endif()
add_compile_options(
# -DVALGRIND
-Wall
-Wno-unused-parameter
-Woverloaded-virtual
-Wpointer-arith
-Wwrite-strings
-O3
-rdynamic
)
...
When I use cmake .. -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_C_COMPILER=clang and make VERBOSE=1, I got some message as follow:
Just as you can see, the -rdynamic compile option did appear in the clang++ command and the compiler also complainted that the argument was unused. But when I used the command below, something strange happended.
clang++ -I/home/phoenix/MyProject/Tmuduo -g -Wthread-safety -Wall -Wno-unused-parameter -Woverloaded-virtual -Wpointer-arith -Wwrite-strings -rdynamic -std=gnu++11 test/Exception_test.cc base/Exception.cc base/CurrentThread.cc -o exception_test -O3
Everything goes well. This time, the -rdynamic option works. That reall make me confuse. Can anyone tell me what's going on here? Why the clang++ command works while the cmake failed?
Because -rdynamic is a linker option, so if you use when compiling a source file into an object *.o it does nothing, there is no link phase here.
When linking all the *.o and libraries into the finally executable, then it is actually used.
From man gcc (but clang uses the same):
-rdynamic
Pass the flag -export-dynamic to the ELF linker, on targets that support it.
This instructs the linker to add all symbols, not only used ones, to the
dynamic symbol table. This option is needed for some uses of "dlopen" or to
allow obtaining backtraces from within a program.
I'm building a library (libproj) using g++-9 and CMake (on Xenial, on Travis), so I can statically link it in a Rust crate. My build.rs sets up and runs CMake with the following config:
cmake proj-7.0.1
-DBUILD_SHARED_LIBS=ON
-DBUILD_TESTING=OFF
-DCMAKE_BUILD_TYPE=Release
-DCMAKE_CXX_FLAGS="-std=c++11 -fPIC"
-DCMAKE_INSTALL_PREFIX=[path snipped]/out
-DCMAKE_C_FLAGS= -ffunction-sections -fdata-sections -fPIC -m64
-DCMAKE_C_COMPILER=/usr/bin/gcc-9
-DCMAKE_CXX_COMPILER=/usr/bin/g++-9
-DCMAKE_ASM_FLAGS= -ffunction-sections -fdata-sections -fPIC -m64
-DCMAKE_ASM_COMPILER=/usr/bin/gcc-9
Which builds and installs libproj successfully.
I then tell cargo to statically link it:
cargo:root=[path snipped]/out
cargo:rustc-link-search=native=[path snipped]/out/lib
cargo:rustc-link-lib=static=proj
However, the link step fails, saying I can't use relocation in a shared object:
note: /usr/bin/ld: [path snipped]/out/lib/libproj.a(4D_api.cpp.o): relocation
R_X86_64_32 against `.rodata.str1.8' can not be used when making a
shared object; recompile with -fPIC`
[path snipped]/out/lib/libproj.a(4D_api.cpp.o): error adding symbols: Bad value
collect2: error: ld returned 1 exit status
What am I doing wrong here? I've also tried to build libproj as a static lib by setting BUILD_SHARED_LIBS to OFF as per the install instructions, but that hasn't had any effect.
UPDATE:
I've managed to enable PIC for g++ (by default it's only enabled for gcc)
running: "cmake" "-Wdev" "--debug-output" "[snipped]/proj-7.0.1" "-DCMAKE_CXX_FLAGS=-std=c++11" "-DCMAKE_CXX_FLAGS=-fPIC" "-DBUILD_SHARED_LIBS=OFF" "-DCMAKE_INSTALL_PREFIX=[snipped]/out" "-DCMAKE_C_FLAGS= -ffunction-sections -fdata-sections -fPIC -m64" "-DCMAKE_C_COMPILER=/usr/bin/gcc-9" "-DCMAKE_CXX_COMPILER=/usr/bin/g++-9" "-DCMAKE_ASM_FLAGS= -ffunction-sections -fdata-sections -fPIC -m64" "-DCMAKE_ASM_COMPILER=/usr/bin/gcc-9" "-DCMAKE_BUILD_TYPE=Debug" "-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON"
Resulting in the following g++-9 invocation, which includes -fPIC:
/usr/bin/g++-9 -DCURL_ENABLED -DMUTEX_pthread -DPROJ_LIB=\"/[snip]/proj\" -DTIFF_ENABLED -I/[snip]/src -I/[snip]/include -I/[snip]/src -I/usr/include/x86_64-linux-gnu -fPIC -g -fvisibility=hidden -Wall -Wextra -Wswitch -Wshadow -Wunused-parameter -Wmissing-declarations -Wformat -Wformat-security -std=c++11 -o CMakeFiles/proj.dir/aasincos.cpp.o -c /[snip]/proj/proj-7.0.1/src/aasincos.cpp
However, I know get a completely different error (thousands of lines), that looks like my static library hasn't been correctly linked to the c++11 stdlib:
Error:
undefined reference to std::allocator<char>::allocator()
UPDATE 2:
I've managed to remove the -nodefaultlibs flag from ld. The latest g++ invocation:
/usr/bin/g++-9 -I/[snipped]/src -I/[snipped]/include -I/[snipped]/src -isystem /[snipped]/include -isystem /[snipped]/googletest -fPIC -g -fvisibility=hidden -Wall -Wextra -Wswitch -Wshadow -Wunused-parameter -Wmissing-declarations -Wformat -Wformat-security -pthread -std=c++11 -o CMakeFiles/proj_context_test.dir/proj_context_test.cpp.o -c /[snipped]/proj_context_test.cpp
And I've managed to add lstdc++ to the linker invocation:
"cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" "-L" "/[snip]/lib" "/home/travis/build/georust/proj-sys/target/debug/deps/proj_sys-95cbd73f2c3f0bde.20tasg77xuhsj5z6.rcgu.o" "/home/travis/build/georust/proj-sys/target/debug/deps/proj_sys-95cbd73f2c3f0bde.57y4784ihm3qw715.rcgu.o" "-o" "/[snip]/deps/proj_sys-95cbd73f2c3f0bde" "/home/travis/build/georust/proj-sys/target/debug/deps/proj_sys-95cbd73f2c3f0bde.3kiafs23968zh4mj.rcgu.o" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro" "-Wl,-znow" "-L" "/home/travis/build/georust/proj-sys/target/debug/deps" "-L" "/home/travis/build/georust/proj-sys/target/debug/build/proj-sys-aff2ac9d43b77886/out/lib" "-L" "/home/travis/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-lsqlite3" "-lcurl" "-ltiff" "-lstdc++" "-Wl,-Bstatic" "-Wl,--whole-archive" "-lproj" "-Wl,--no-whole-archive" [trimmed rlib details] "-Wl,--end-group" "/home/travis/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-2541f1e09df1c67d.rlib" "-Wl,-Bdynamic" "-ldl" "-lrt" "-lpthread" "-lgcc_s" "-lc" "-lm" "-lrt" "-lpthread" "-lutil" "-lutil"
But the undefined reference error remains.
As I can see from your example, you have quotation marks around CMAKE_CXX_FLAGS and do not have ones around CMAKE_C_FLAGS. It looks odd.
I have built libgproj myself with cmake + your flags and with make VERBOSE=1.
I can see, that C-files compiled without fPIC, m64 and fdata-sections. I suppose, that the cause is in the quotation marks around CMAKE_C_FLAGS. I saw your log in Travis, and there is configuration for cmake from cargo:
"cmake" "/home/travis/build/georust/proj-sys/PROJSRC/proj/proj-7.0.1" "-DBUILD_SHARED_LIBS=ON" "-DBUILD_TESTING=OFF" "-DCMAKE_BUILD_TYPE=Release" "-DCMAKE_CXX_FLAGS=-std=c++11 -fPIC" "-DCMAKE_INSTALL_PREFIX=/home/travis/build/georust/proj-sys/target/debug/build/proj-sys-03a5fe6428bb060a/out" "-DCMAKE_C_FLAGS= -ffunction-sections -fdata-sections -fPIC -m64" "-DCMAKE_C_COMPILER=/usr/bin/gcc-9" "-DCMAKE_CXX_COMPILER=/usr/bin/g++-9" "-DCMAKE_ASM_FLAGS= -ffunction-sections -fdata-sections -fPIC -m64" "-DCMAKE_ASM_COMPILER=/usr/bin/gcc-9"
Note, that -DCMAKE_C_FLAGS and CMAKE_CXX_FLAGS escaped with their values, and not values by itself. I suppose, that these options become in the shell:
cmake -DCMAKE_C_FLAGS= -ffunction-sections -fdata-sections -fPIC -m64 -DCVMAKE_CXX_FLAGS=-std=c++11 -fPIC
I have tried this command, without quotation marks and cmake just ignored unknown options. So it will not fail.
I suggest you to add cmake option -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON to cmake configuration or VERBOSE=1 to the make options, if you can, and then you can see real compile options passed to gcc in Travis.
You will see somethig like this:
[ 19%] Building C object src/CMakeFiles/proj.dir/wkt1_generated_parser.c.o
cd [some path]/PROJ/cmake_build/src && /usr/bin/cc -DCURL_ENABLED -DMUTEX_pthread
-DPROJ_LIB=\"/usr/local/share/proj\" -DTIFF_ENABLED -I[some path]PROJ/src -I[some
path]/PROJ/include -I[some path]/PROJ/cmake_build/src -O3 -DNDEBUG -
fvisibility=hidden -Wall -Wextra -Wswitch -Wshadow -Wunused-parameter -Wmissing-
declarations -Wformat -Wformat-security -Wmissing-prototypes -std=c99 -o
CMakeFiles/proj.dir/wkt1_generated_parser.c.o -c [some
path]/PROJ/src/wkt1_generated_parser.c
Conclusion:
You can try adding verbosity to see real compile options.
You can try to escape options properly, if it's necessary after first step.
Also I want to note, that SHARED_BUILD_LIBS=ON in my test forbids building static lib, which you want to link. When I set shared libs building setting option to OFF, static lib was built.
I built boringssl with cmake and msvc
Then I tried to build with clang-cl so I used -T"LLVM-vs2014" in vmake arguments
Clang-cl uses cl arguments however cmake used gcc style arguments without adding -Xclang
The proper way to handle this case is checking for
(${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang" AND "x${CMAKE_CXX_SIMULATE_ID}" STREQUAL "xMSVC"))
the problem was here :
if(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wshadow -Werror -Wno-error=shadow -Wno-error=unused-value -ggdb -std=gnu89")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wshadow -Werror -Wno-error=shadow -Wno-error=unused-value -ggdb -std=c++0x")
I changed Clang to Clng so clang-cl wasn't identified as clang.exe which take gcc style arguments
My cmake file:
cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_COMPILER "gcc-7")
project(invertedindex)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wconversion -O0")
set(SOURCE_FILES main.cpp inverted_index.hpp params.hpp)
add_executable(invertedindex ${SOURCE_FILES})
Fortunately I looked at the generated compiler command
/usr/local/bin/gcc-7 -Wall -Wextra -Wconversion -O0 -std=gnu++1z -o CMakeFiles/invertedindex.dir/main.cpp.o -c /Users/adam/school/cpp/invertedindex/main.cpp
and saw -std=gnu++1z. I have no interest in studying long hours what the f*ck that is and why it is there when I have
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
It suspect that -std=gnu++1z is not a stable, finished library, or sth. But gcc 7 supports most of the features of c++17 standard.
when I chickened out of knowing that it should work, I replaced manually the generated command parameter to -std=gnu++17 and it worked. What's wrong? How do I make it work in cmake?
See how to specify compiler in cmake
I was using the third method which is marked with avoid.
First method, that is setting CXX env var didn't work (compiler was some Mac g++).
So I tried the second method - specifying compiler in cmake args -D CMAKE_C_COMPILER=gcc-7, which worked. Even though generating again c++1z param. I don't understand it, but until I breaks I'm good.
/usr/local/bin/gcc-7 -Wall -Wextra -Wconversion -O0 -g -std=gnu++1z -o CMakeFiles/invertedindex.dir/main.cpp.o -c /Users/adam/school/cpp/invertedindex/main.cpp
I've been unable to find a definitive answer, so apologies if the answer is out there.
I use cmake to generate project files as I tend to work cross-platform. On OSX/Xcode, I'm seeing unwanted compiler flags being used, specifically warning enable/disable. I like to compile with as many warnings on as possible, and set warnings as errors.
Consider the following:
File: CMakeLists.txt
cmake_minimum_required( VERSION 3.0 )
project( test )
set( CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -std=c++14 )
add_compile_options( "-Werror" )
add_compile_options( "-Weverything" )
add_compile_options( "-Wno-c++98-compat-pedantic" )
add_compile_options( "-Wno-padded" )
add_executable( Test main.cpp )
target_link_libraries( Test libstdc++.dylib libc++.dylib )
File: main.cpp
#include <iostream>
int main( int argc, char * const argv[] )
{
(void)argc;
(void)argv;
std::cout << "Kittens." << std::endl;
return 0;
}
If I then run cmake:
cmake -G Xcode .
(normally I'd use a builds directory, but not here for simplicity).
Then build (from the command line):
cmake --build . --config Debug --target ALL_BUILD
The simple sample will compile. If I now look at the command used to compile main.cpp I see the following:
CompileC test.build/Debug/Test.build/Objects-normal/x86_64/main.o main.cpp normal x86_64 c++ com.apple.compilers.llvm.clang.1_0.compiler
cd /Users/username/Development/test
export LANG=en_US.US-ASCII
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -x c++ -arch x86_64 -fmessage-length=101 -fdiagnostics-show-note-include-stack -fmacro-backtrace-limit=0 -fcolor-diagnostics -Wno-trigraphs -fpascal-strings -O0 -Wno-missing-field-initializers -Wno-missing-prototypes -Wno-return-type -Wno-non-virtual-dtor -Wno-overloaded-virtual -Wno-exit-time-destructors -Wno-missing-braces -Wparentheses -Wswitch -Wno-unused-function -Wno-unused-label -Wno-unused-parameter -Wno-unused-variable -Wunused-value -Wno-empty-body -Wno-uninitialized -Wno-unknown-pragmas -Wno-shadow -Wno-four-char-constants -Wno-conversion -Wno-constant-conversion -Wno-int-conversion -Wno-bool-conversion -Wno-enum-conversion -Wno-shorten-64-to-32 -Wno-newline-eof -Wno-c++11-extensions -DCMAKE_INTDIR=\"Debug\" -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -fasm-blocks -fstrict-aliasing -Wdeprecated-declarations -Winvalid-offsetof -mmacosx-version-min=10.11 -g -Wno-sign-conversion -I/Users/username/Development/test/Debug/include -I/Users/username/Development/test/test.build/Debug/Test.build/DerivedSources/x86_64 -I/Users/username/Development/test/test.build/Debug/Test.build/DerivedSources -Wmost -Wno-four-char-constants -Wno-unknown-pragmas -F/Users/username/Development/test/Debug -std=c++14 -Werror -Weverything -Wno-c++98-compat-pedantic -Wno-padded -MMD -MT dependencies -MF /Users/username/Development/test/test.build/Debug/Test.build/Objects-normal/x86_64/main.d --serialize-diagnostics /Users/username/Development/test/test.build/Debug/Test.build/Objects-normal/x86_64/main.dia -c /Users/username/Development/test/main.cpp -o /Users/username/Development/test/test.build/Debug/Test.build/Objects-normal/x86_64/main.o
The really annoying thing here is any of the -W options.
In my CMakeLists.txt I've clearly said I want all warnings, warnings treated as errors, and whitelisted a couple of warnings.
However, if I strip out all the -W options in the compile command, I see (in addition to what I asked for):
-Wno-trigraphs
-Wno-missing-field-initializers
-Wno-missing-prototypes
-Wno-return-type
-Wno-non-virtual-dtor
-Wno-overloaded-virtual
-Wno-exit-time-destructors
-Wno-missing-braces
-Wparentheses
-Wswitch
-Wno-unused-function
-Wno-unused-label
-Wno-unused-parameter
-Wno-unused-variable
-Wunused-value
-Wno-empty-body
-Wno-uninitialized
-Wno-unknown-pragmas
-Wno-shadow
-Wno-four-char-constants
-Wno-conversion
-Wno-constant-conversion
-Wno-int-conversion
-Wno-bool-conversion
-Wno-enum-conversion
-Wno-shorten-64-to-32
-Wno-newline-eof
-Wno-c++11-extensions
-Winvalid-offsetof
-Wno-sign-conversion
-Wmost
-Wno-four-char-constants
-Wno-unknown-pragmas
Some of those things listed I really do not want disabled. I really want just the -W options I specified in the cmake file being used for compilation, but I can't see who is injecting all these extra things and from where.
Is there any way I can get cmake to tell xcode not to include all these extra -W options from compilation?
Note: using:
cmake 3.5.2
Apple LLVM version 7.3.0 (clang-703.0.31)
At project() call, CMake detects compiler (you may see corresponded messages when cmake is executed) and sets default flags for it. These flags are stored in variables CMAKE_CXX_FLAGS (common) and CMAKE_CXX_FLAGS_<CONFIG> (configuration-specific).
You may modify these variables as you want:
1) completely rewrite them
set(CMAKE_CXX_FLAGS "<desired-value>")
2) append new flags to them
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} <additional-flags>")
3) remove specific values from them
string(REPLACE "<unwanted-flag>" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
# or
string(REGEX REPLACE "<unwanted-flag-regex>" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
and so on.