1

I have following setting in my Toolchain.cmake

set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_SYSTEM_PROCESSOR x86_64)

set(cross_triple "x86_64-linux-gnu")
set(clang_5.0 "/third_party/llvm-build/Release+Asserts")
set(clang_link_flags "-lstdc++")

set(CMAKE_C_COMPILER ${clang_5.0}/bin/clang)
set(CMAKE_CXX_COMPILER ${clang_5.0}/bin/clang++)
set(CMAKE_ASM_COMPILER ${CMAKE_C_COMPILER})
set(CMAKE_CXX_FLAGS "-std=c++11 -stdlib=libc++ -Wno-c++11-narrowing -v")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${clang_link_flags}")

And, my clang compiler folder as belows:

-rwxr-xr-x 1 1024 1024 15774563 May 11 21:06 sancov
-rwxr-xr-x 1 1024 1024  3925178 May 11 21:06 llvm-symbolizer
-rwxr-xr-x 1 1024 1024 16048083 May 11 21:06 llvm-ar
-rwxr-xr-x 1 1024 1024 63993992 May 11 21:06 lld
-rwxr-xr-x 1 1024 1024 82308936 May 11 21:06 clang
lrwxrwxrwx 1 1024 1024        3 May 18 01:11 ld.lld -> lld
lrwxrwxrwx 1 1024 1024        5 May 18 01:11 clang-cl -> clang
lrwxrwxrwx 1 1024 1024        5 May 18 01:11 clang++ -> clang

Everything looks fine for me. But, when I use Cmake then make to compile my project, for all the .cpp .c files, Cmake choose to use clang instead of clang++ to compile the source codes, like:

"/third_party/llvm-build/Release+Asserts/bin/clang" -cc1 -x c++ xxx.cpp -o xxx.cpp.o

For some reason, the "-lstdc++" flags won't take affect in the linker, which cause some issues.

My questions:

  1. why cmake choose to use "clang -x c++", instead of directly using clang++? Anything missing here? How to let cmake choose clang++?

  2. define "-lstdc++" in CMAKE_EXE_LINKER_FLAGS look like does not take affect. Can you tell what can be the reason?

Thanks.

csc372
  • 21
  • 2
  • The second question is easily searched. E.g. http://stackoverflow.com/questions/32698580/cmake-global-linker-flag-setting-for-all-targets-in-directory. – Tsyvarev May 18 '17 at 18:24

1 Answers1

0

It shouldn't be a problem that CMake uses clang -x c++, it's just telling the clang front end to compile the source as C++ anyway. Rather than explicitly specifying the -std=c++11 -stdlib=libc++ flags, you probably should instead be looking to use the following in your project's CMakeLists.txt file if it needs C++11 support:

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

You can find a detailed discussion of how to set up for C++11 and later here.

As for the remaining compiler and linker flags you are trying to set, you might want to check out CMAKE_<LANG>_FLAGS_INIT and CMAKE_EXE_LINKER_FLAGS_INIT as alternative ways of setting those. They are intended for use in toolchain files to do the sort of thing you are trying to do (but if you use the C++11 method above, you won't need to change the linker flags at all).

Craig Scott
  • 9,238
  • 5
  • 56
  • 85