CMake链接外部库

des*_*ius 5 cmake

首先,我是CMake的新手.我刚刚开始使用它.我想将外部库链接到我的项目.我使用了从CMake wiki中获取的代码(在文章末尾).这是我的CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)

project(hello_world)

set(SOURCE_EXE main.cpp)

include_directories(foo)

add_library(foo STATIC IMPORTED)
set_property(TARGET foo PROPERTY IMPORTED_LOCATION /usr/lib/libfoo.a)

target_link_libraries(main foo)
Run Code Online (Sandbox Code Playgroud)

这是一个错误的文本:

-- The C compiler identification is GNU 4.7.3
-- The CXX compiler identification is GNU 4.7.3
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
CMake Error at CMakeLists.txt:24 (target_link_libraries):
  Cannot specify link libraries for target "main" which is not built by this
  project.


-- Configuring incomplete, errors occurred!
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能正确?

Fra*_*ser 8

看起来你只是错过了一个add_executable电话.您需要main在CMakeLists.txt中添加可执行目标:

set(SOURCE_EXE main.cpp)
add_executable(main ${SOURCE_EXE})
...
target_link_libraries(main foo)
Run Code Online (Sandbox Code Playgroud)