cmake detects clang-cl as clang - c++

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

Related

How to print out debug info to terminal when running c++ with CMake?

I am reading a C++ project and want to print out the #ifdef _DEBUG message when running the program at a Linux terminal. For example:
#ifdef _DEBUG
cout << s1 << endl;
#endif
Currently, it doesn't print out the debug info above, but only prints out logger info as below:
logger_(MY_MODULE_LOG_ERROR, "config is null ");
The project is made through cmake. It has a top level CMakeLists.txt file, in addition to each CZMakeLists.txt under src/ and its subdirectories. The content of the top-level CMakelists.txt is below:
cmake_minimum_required (VERSION 3.12)
project (TAGS_NEW )
set(CMAKE_VERBOSE_MAKEFILE true)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -fPIC -w")
# gdb debug : -g
add_compile_options(
-Wall
-Wextra
-Wstrict-aliasing
-Wno-unused-parameter
-Wno-missing-field-initializers
-Wchar-subscripts
-Wpointer-arith
-Wformat
-Wformat-security
-Werror=format-security
-fstack-protector-all
-fPIE
-fpie
-fPIC
-fpic
-pipe
-fdata-sections
-ffunction-sections
)
option(DEBUG_OUTPUT "option for debug out" OFF)
if (DEBUG_OUTPUT)
add_definitions(-D_DEBUG)
endif()
# option(DEBUG_GDB "option for gdb debug" OFF)
# if (DEBUG_GDB)
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -ggdb")
# endif()
# set(CMAKE_BUILD_TYPE "Debug")
option(CMAKE_BUILD_TYPE "option for gdb debug" DEBUG)
set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb")
set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
option(COMPILE_DLL "option for generate dynamic library" OFF)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${TAGS_NEW_SOURCE_DIR}/output)
add_definitions(-D_Python_CALL -D_ChaiScriptON)
include_directories(${TAGS_NEW_SOURCE_DIR}/third-party/include ${TAGS_MINING_NEW_SOURCE_DIR}/include )
# $(python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())")
find_package( PythonLibs 2.7 REQUIRED )
include_directories( ${PYTHON_INCLUDE_DIRS} )
link_directories(${TAGS_NEW_SOURCE_DIR}/third-party/lib)
add_subdirectory (src)
execute_process(COMMAND sh genGlobal.sh ${TAGS_NEW_SOURCE_DIR} WORKING_DIRECTORY ${TAGS_NEW_SOURCE_DIR})
I tried to change the OFF to ON in the line below, but it doesn't help:
option(DEBUG_OUTPUT "option for debug out" OFF)
I am new to CMake. How to print out all the debug info?
Options are meant to be provided from outside not to be modified in the CMake file. In fact you can't change an option with the option command once it is set in the cache (after the first CMake run). So run your cmake like this: cmake -DDEBUG_OUTPUT=ON .. and you will get your macro defined.

clang: warning: argument unused during compilation: '-rdynamic'

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.

error: no type named 'invoke_result_t' in namespace 'std' gcc-7

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

Change the order of compiler flags

To prevent "undefined reference to..." boost errors, I need to append the boost libraries at the very end of the compiler flags. Therefore, in CMakeLists.txt I set:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pedantic -O0 -Wall -DBOOST_SYSTEM_NO_DEPRECATED -lboost_system -lboost_filesystem")
However, verbose output of cmake shows me that additional flags are appended behind the ones I defined:
g++ -std=c++11 -pedantic -O0 -Wall -DBOOST_SYSTEM_NO_DEPRECATED -lboost_system -lboost_filesystem CMakeFiles/My_Project.dir/main.cpp.o -o My_Project -L/usr/local/boost_1_60_0/lib
Is it possible to change the order?
The complete CMakeLists.txt:
cmake_minimum_required(VERSION 3.4)
project(My_Project)
set(CMAKE_VERBOSE_MAKEFILE ON)
# This is bad but I currently don't have another working solution.
set(BOOSTROOT "/usr/local/boost_1_60_0/")
set(BOOST_ROOT "/usr/local/boost_1_60_0/")
find_package(Boost 1.60.0 COMPONENTS system filesystem REQUIRED)
if(Boost_FOUND)
message(STATUS "Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
message(STATUS "Boost_LIBRARIES: ${Boost_LIBRARIES}")
message(STATUS "Boost_VERSION: ${Boost_VERSION}")
link_directories(${Boost_LIBRARY_DIRS})
include_directories(${Boost_INCLUDE_DIRS})
endif()
add_executable(BoostTest main.cpp)
if(Boost_FOUND)
target_link_libraries(BoostTest ${Boost_LIBRARIES})
endif()
# Boost libraries appended at the end. However, cmake generates flags like this:
# c++ -std=c++11 -pedantic -O0 -Wall -DBOOST_SYSTEM_NO_DEPRECATED -lboost_system -lboost_filesystem CMakeFiles/My_Project.dir/main.cpp.o -o My_Project -L/usr/local/boost_1_60_0/lib
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pedantic -O0 -Wall -DBOOST_SYSTEM_NO_DEPRECATED -lboost_system -lboost_filesystem")
set(SOURCE_FILES main.cpp)
add_executable(My_Project ${SOURCE_FILES})
Thanks
You should use target_link_libraries instead of manually appending -lboost directives into your compiler flags.
TARGET_LINK_LIBRARIES(My_Project boost)
It should also be mentioned that it's possible the linker might be invoked as a separate invocation after the compilation of object files

CMake clang and c++0x

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.