CMake链接一个外部库 [英] CMake link an external library

查看:275
本文介绍了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)

这里是一个错误的文本:

   -  C编译器标识是GNU 4.7.3 
- CXX编译器标识是GNU 4.7.3
- 检查工作C编译器:/ usr / bin / cc
- 检查工作C编译器:/ usr / bin / cc - works
- 检测C编译器ABI info
- 检测C编译器ABI info - done
- 检查CXX编译器是否正常:/ usr / bin / c ++
- 检查CXX编译器是否正常工作:/ usr / bin / c ++ - works
- 检测CXX编译器ABI info
- 检测CXX编译器ABI信息 - done
CMakeLists.txt中的CMake错误:24(target_link_libraries):
无法为目标main指定不是由此
项目构建的链接库。


- 配置不完整,发生错误!

如何正确地做?

解决方案

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

  set(SOURCE_EXE main.cpp)
add_executable(main $ {SOURCE_EXE})
...
target_link_libraries(main foo)
pre>

First of all, I am newbie in CMake. I've just started work with it. I want to link an external library to my project. I use code which I take from CMake wiki (at the end of article). Here is my 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)

And here is a text of error:

-- 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!

How can I do it correctly?

解决方案

It looks like you're just missing out an add_executable call. You need to add main as an executable target in your CMakeLists.txt:

set(SOURCE_EXE main.cpp)
add_executable(main ${SOURCE_EXE})
...
target_link_libraries(main foo)

这篇关于CMake链接一个外部库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆