简体   繁体   中英

How to add the option `llvm-config --cxxflags --ldflags --libs` in CMake?

Does anyone know how to add the option `llvm-config --cxxflags --ldflags --libs` into CMake? The tricky part for me is the backtick ` .

I need to config my CMake files to obtain a compilation command like:

g++  test.cpp -lclangBasic -I/usr/lib/llvm-6.0/include 
     -Wall `llvm-config --cxxflags --ldflags --libs` 

I tried to use the following options, but they don't work:

add_compile_options(`llvm-config --cxxflags --ldflags --libs`)
# or
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} `llvm-config --cxxflags --ldflags --libs`")

Thank you in advance.

===============

Update 1.

Using the output from `llvm-config --cxxflags --ldflags --libs` , I can compile successfully with the following command:

g++  test.cpp -lclangBasic -I/usr/lib/llvm-6.0/include  
-Wall -I/usr/lib/llvm-6.0/include -L/usr/lib/llvm-6.0/lib -lLLVM-6.0

I can pass -I/usr/lib/llvm-6.0/include by using include_directories(usr/lib/llvm-6.0/include) .

But still, I don't know how to pass the part -L/usr/lib/llvm-6.0/lib -lLLVM-6.0 to CMake. Using link_directories and target_link_libraries like the following doesn't work for me:

link_directories(/usr/lib/llvm-6.0/lib)
target_link_libraries(test PUBLIC "LLVM-6.0")

Does anyone know to make them work in CMake?

===============

Update 2.

I have to add the following code into the file CMakeLists.txt to make CMake work.

add_library(LLVM-6.0 SHARED IMPORTED) # or STATIC instead of SHARED
set_target_properties(LLVM-6.0 PROPERTIES
  IMPORTED_LOCATION "/usr/lib/llvm-6.0/lib/libLLVM-6.0.so"
  INTERFACE_INCLUDE_DIRECTORIES "/usr/lib/llvm-6.0/include"
)
add_library(clangBasic SHARED IMPORTED) # or STATIC instead of SHARED
set_target_properties(clangBasic PROPERTIES
  IMPORTED_LOCATION "/usr/lib/llvm-6.0/lib/libclangBasic.a"
  INTERFACE_INCLUDE_DIRECTORIES "/usr/lib/llvm-6.0/include"
)
target_link_libraries(solidity PUBLIC "LLVM-6.0;clangBasic")

However, this looks manually, and I'm still looking for better solutions...

LLVM/Clang use CMake and provide a config module , so any project trying to use LLVM/Clang just need a find_package(Clang...) to bring the exported targets into a scope (and don't need to use any external tools (like llvm-config or pkg-config blah-blah )). Meaning that after Clang has found, you can use target_link_libraries(blah Clang::clangBasic ...) and no need to do any "manual import" or whatever ppl suggest in the comments...

As a quick check one may use the following command:

$ cmake --find-package -DNAME=Clang -DCOMPILER_ID=GNU -DLANGUAGE=CXX -DMODE=EXIST
Clang found.
$ cmake --find-package -DNAME=Clang -DCOMPILER_ID=GNU -DLANGUAGE=CXX -DMODE=COMPILE
-I/usr/lib/llvm-6.0/include

I used Clang 6.0 installed from the deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial-6.0 main repo.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM