add RxCpp library to cmake project

2019-07-13 07:27发布

I'd like to use RxCpp on Linux (gcc), but I have trouble to add RxCpp library appropriately to my cmake project. I followed cloning and building instructions and ran:

git clone --recursive https://github.com/Reactive-Extensions/RxCpp.git
cd RxCpp

mkdir projects/build
cd projects/build
cmake -G"Unix Makefiles" -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DCMAKE_BUILD_TYPE=RelWithDebInfo -B. ../CMake
make

I tried to run make install but got:

make: *** No rule to make target 'install'.  Stop.

Then, I noticed the file libRxCpp.so in my build directory and copied it:

cp libRxCpp.so /sw/RxCpp/2.3.0/

I created a new project in Clion, which uses cmake and modified CMakeLists.txt like this:

cmake_minimum_required(VERSION 3.6)
project(RxCpp_Example)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

# my try to add library RxCpp
set(RxCpp_Lib_PATH /sw/RxCpp/2.3.0/)    
find_library(RxCpp_Lib NAMES libRxCpp.so PATHS ${RxCpp_Lib_PATH})
target_link_libraries(RxCpp_Example ${RxCpp_Lib})

set(SOURCE_FILES main.cpp)
add_executable(RxCpp_Example ${SOURCE_FILES})

However, I get an error when updating the cmake project (or running cmake in terminal):

CMake Error at CMakeLists.txt:9 (target_link_libraries):
  Cannot specify link libraries for target "RxCpp_Example" which is not built
  by this project.

What am I doing wrong? How can I add RxCpp library to my cmake project?

1条回答
在下西门庆
2楼-- · 2019-07-13 08:15

You called target_link_libraries before creating the target with add_executable. Change your cmake file like this:

find_library(RxCpp_Lib NAMES libRxCpp.so PATHS ${RxCpp_Lib_PATH})

set(SOURCE_FILES main.cpp)
add_executable(RxCpp_Example ${SOURCE_FILES})

target_link_libraries(RxCpp_Example ${RxCpp_Lib})
查看更多
登录 后发表回答