【发布时间】:2018-03-12 17:21:50
【问题描述】:
我想从我的 C++ 源代码(测试和发布)中获取两个可执行文件。我在两个单独的 C++ 文件中有两个 main() 函数。
使用Meson 构建系统很容易:
project('PrjName', 'cpp')
mainSrc = ['header1.hpp', 'source1.cpp', 'source2.cpp']
testSrc = ['header2.hpp', 'source2.cpp', 'test.cpp']
mainExe = executable('prjName', mainSrc)
testExe = executable('prjNameTest', testSrc)
CMake 无法得到相同的结果:
cmake_minimum_required(VERSION 3.10)
project("PrjName")
set(SOURCES
"header1.hpp"
"source1.cpp"
"source2.cpp"
)
set(TEST_SOURCES
"header2.hpp"
"source2.cpp"
"test.cpp"
)
add_executable("prjName" ${SOURCES})
add_executable("prjNameTest" ${TEST_SOURCES})
我得到了第一个可执行文件 (prjName),但不是第二个,但出现错误:
'main'的多重定义
但是main()函数是在“source1.cpp”和“test.cpp”中定义的,所以应该没有冲突。
考虑到从 Meson 构建看起来代码应该没问题,我该如何解决这个问题?
【问题讨论】:
-
以minimal reproducible example的形式显示
source1.cpp、source2.cpp和header1.hpp中的代码。I get the first executable (prjName), but not the second, with the error:- 通常,在第一个可执行文件完全构建之前,第二个可执行文件不会开始编译。如果问题不取决于第二个可执行文件,请将其从CMakeLists.txt中删除,然后重试。 -
把你的真实脚本放在这里。很明显介子项目中的文件与CMake项目不同
-
例如,TEST_SOURCES 有“source2.cpp”,而它只有“source1.cpp”用于介子系统
-
@Nibor:我修正了错字。谢谢。
-
您想从我们这里得到什么?您的
CMakeLists.txt脚本正确。 (假设您要将位于源目录(靠近CMakeLists.txt)中的“source2.cpp”和“test.cpp”编译到第二个可执行文件中。这可能是源文件本身的问题,但您没有不给他们看。
标签: c++ cmake executable meson-build