What Is the Idiomatic Way in Cmake to Add the -Fpic Compiler Option

Pass compound compiler options using cmake

What you observe here is not CMake preserving quotes.

CMake has special kind of strings - a list. A list is a string, in which elements are delimited with ";". When you write somefunction(A B C) you actually constructing a string "A;B;C", which is also a list of 3 elements. But when you write somefunction("A B C") you are passing a plain string.

Now, when you write add_compile_options("-mllvm -XYZ;-mllvm -ABC"), CMake does correct thing (in a sense) - it passes compiler two command-line arguments, "-mllvm -XYZ" and "-mllvm -ABC". The problem is that clang gets these arguments as two too. That is, argv[i] in clang's main() points to "-mllvm -XYZ" string and clang treats it as single flag.

The only solution i can see is to use CMAKE_{C,CXX}_FLAGS to set these flags. As @Tsyvarev said, it's perfectly ok to use this variable.

How to enable `/std:c++latest` in cmake?

Until CMake 3.20.3, if you ask for C++20, using set(CMAKE_CXX_STANDARD 20), you'll get -std:c++latest. Proof:

  if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 19.12.25835)
set(CMAKE_CXX20_STANDARD_COMPILE_OPTION "-std:c++latest")
set(CMAKE_CXX20_EXTENSION_COMPILE_OPTION "-std:c++latest")
endif()

(from Modules/Compiler/MSVC-CXX.cmake in the cmake sources)

UPDATE: Since CMake 3.20.4, set(CMAKE_CXX_STANDARD 20) gets you -std:c++20, so you need set(CMAKE_CXX_STANDARD 23) to get -std:c++latest -- assuming that your MSVC compiler version is 16.11 Preview 1 or later (see cmake commit 3aaf1d91bf353).

How can I have CMake compile the same input file in two different languages?

You could create library targets mycpplib and myclib in the different directories (in the different CMakeLists.txt). That way you may call set_source_files_properties in the directory where mycpplib library is created, and that call won't affect on myclib.

There are also DIRECTORY and TARGET_DIRECTORY options for command set_source_files_properties, which could affect on the directory where the property will be visible:

# In 'c/CMakeLists.txt`
# add_library(myclib ${CMAKE_SOURCE_DIR}/foo.bar)
# In 'cpp/CMakeLists.txt`
# add_library(mycpplib ${CMAKE_SOURCE_DIR}/foo.bar)
# In CMakeLists.txt
add_subdirectory(c)
add_subdirectory(cpp)
set_source_file_properties(foo.bar TARGET_DIRECTORY myclib
PROPERTIES LANGUAGE C)
set_source_file_properties(foo.bar TARGET_DIRECTORY mycpplib
PROPERTIES LANGUAGE CXX)


Related Topics



Leave a reply



Submit