如何告诉 CMake 链接源目录中的静态库? [英] How do I tell CMake to link in a static library in the source directory?

查看:23
本文介绍了如何告诉 CMake 链接源目录中的静态库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 Makefile 的小项目,我正在尝试将其转换为 CMake,主要是为了获得 CMake 的经验.出于本示例的目的,该项目包含一个源文件(C++,但我认为该语言不是特别相关)和一个我从别处复制的静态库文件.为了论证,假设库的源代码不可用;我只有 .a 文件和相应的标题.

I have a small project with a Makefile which I'm trying to convert to CMake, mostly just to get experience with CMake. For purposes of this example, the project contains a source file (C++, though I don't think the language is particularly relevant) and a static library file which I've copied from elsewhere. Assume for argument's sake that the source code to the library is unavailable; I only have the .a file and the corresponding header.

我手工制作的 Makefile 包含此构建规则:

My handmade Makefile contains this build rule:

main: main.o libbingitup.a
    g++ -o main main.o libbingitup.a

效果很好.我如何告诉 CMake 重现这个?当然,不是字面上的这个确切的 makefile,而是包含等效链接命令的东西.我已经尝试过明显但天真的方法,比如

which works fine. How do I tell CMake to reproduce this? Not literally this exact makefile, of course, but something that includes an equivalent linking command. I've tried the obvious but naive ways, like

add_executable(main main.cpp libbingitup.a)

add_executable(main main.cpp)
target_link_libraries(main libbingitup.a)

以及 link_directories(.)add_library(bingitup STATIC IMPORTED) 等的各种东西,但到目前为止没有任何东西可以成功链接.我该怎么办?

as well as various things with link_directories(.) or add_library(bingitup STATIC IMPORTED) etc. but nothing so far that results in a successful linkage. What should I be doing?

版本详情:Linux (Kubuntu 12.04) 上的 CMake 2.8.7 和 GCC 4.6.3

Version details: CMake 2.8.7 on Linux (Kubuntu 12.04) with GCC 4.6.3

推荐答案

CMake 倾向于将完整路径传递给链接库,因此假设 libbingitup.a 在 ${CMAKE_SOURCE_DIR} 中,执行以下操作应该成功:

CMake favours passing the full path to link libraries, so assuming libbingitup.a is in ${CMAKE_SOURCE_DIR}, doing the following should succeed:

add_executable(main main.cpp)
target_link_libraries(main ${CMAKE_SOURCE_DIR}/libbingitup.a)

这篇关于如何告诉 CMake 链接源目录中的静态库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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