如何告诉 CMake 链接源目录中的静态库?

编程入门 行业动态 更新时间:2024-10-20 03:17:10
本文介绍了如何告诉 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屋!

更多推荐

[db:关键词]

本文发布于:2023-03-31 05:15:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/795970.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:静态   链接   目录中   CMake

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!