如何将一个 exe 项目链接到另一个 exe 项目中的类



>假设你想对可执行文件中的类进行一些单元测试,但你不想将它们重构到一个库中,你可以在其中使用 cmake 中的target_link_libraries( target library )添加库。

如何授予测试类对其他类的访问权限?

1( 使用其他项目的源文件构建测试项目? 另一件事

include_directories(${otherExeProjectDir})
set( SOURCE_FILES 
main.cpp
tests.h
tests.cpp
${otherExeProjectDir}/otherclass1.h
${otherExeProjectDir}/otherclass2.h
)

2( 将测试项目与其他项目中的 obj 文件链接? 某种add_library( otherclass.obj )疯狂?

3(

如果您的主要可执行源位置是简单的或平面的,那么这样的东西可以工作:

cmake_minimum_required(VERSION 3.9)
project(tests)
# Get main executable source location properties
get_target_property(exe_sources exe SOURCES)
get_target_property(exe_source_dir exe SOURCE_DIR)
# Remove main entry point file
list(REMOVE_ITEM exe_sources main.cpp)
# Add test sources
add_executable(test1 test1.cpp)
# Add exe sources to test (assumes sources are relative paths)
foreach(src IN LISTS exe_sources)
target_sources(test1 PRIVATE "${exe_source_dir}/${src}")
endforeach()
# Add exe include directories to test
target_include_directories(test1 PRIVATE 
${exe_source_dir}
$<TARGET_PROPERTY:exe,INCLUDE_DIRECTORIES>)

否则,不幸的是,一般的解决方案是依赖一些外部信息,例如顶级源文件位置或将您自己的源属性添加到主可执行目标。

最新更新