CMake:是否可以仅从静态库而不使用源构建可执行文件? [英] CMake: Is it possible to build an executable from only static libraries and no source?

查看:19
本文介绍了CMake:是否可以仅从静态库而不使用源构建可执行文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想从静态库(即 .a 文件)构建可执行文件.这是可能的,因为 main() 函数包含在这些库之一中.

I would like to build an executable from static libraries (i. e. .a-files) only. This is possible, because the main() function is contained in one of these libraries.

add_executable() 函数要求我至少提供一个源文件.但这不是我想做的.

The add_executable() function requires me to provide at least one source file. But this is not what I want to do.

推荐答案

没有黑客就没有办法做到这一点.您至少需要一个 *.c 或 *.cpp 文件.

There is no way to do it without a hack. You need at least one *.c or *.cpp file.

我所做的是制作一个虚拟的 null.cpp 文件(零字节)并使用它.你也可以使用 /dev/null 但它只适用于 Linux.

What I do is make a dummy null.cpp file (zero bytes) and use that. You can also use /dev/null but that only works on Linux.

file(WRITE null.cpp "")

add_executable(tester
    null.cpp
)

target_link_libraries(tester
    -Wl,--whole-archive
    libtest1
    libtest2
    libtest3
    libtest4
    -Wl,--no-whole-archive
    gtest_main
)

这篇关于CMake:是否可以仅从静态库而不使用源构建可执行文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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