ssb*_*ssb 62 c++ executable build cmake
我在C++项目中的代码组织如下
.cpp和.h文件,其中包含我的班.cxx文件必须针对.cpp文件和一些外部库进行编译.现在,每个.cxx文件都有一个main()方法,所以我需要为这些文件中的每个文件添加一个不同的可执行文件.
此外,这些.cxx文件可能无法链接到相同的外部库.
我想在CMake中编写这个版本,我是一个新手,我该如何解决这个问题?
And*_*dré 93
我的建议是分两个阶段解决这个问题:
.cpp和从.h文件构建库add_library.cxx文件并使用add_executable和创建每个文件的可执行文件foreach这可能很简单
file( GLOB LIB_SOURCES lib/*.cpp )
file( GLOB LIB_HEADERS lib/*.h )
add_library( YourLib ${LIB_SOURCES} ${LIB_HEADERS} )
Run Code Online (Sandbox Code Playgroud)
只需遍历所有.cpp文件并创建单独的可执行文件.
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
# with full pathname. RELATIVE may makes it easier to extract an executable name
# automatically.
# file( GLOB APP_SOURCES RELATIVE app/*.cxx )
file( GLOB APP_SOURCES app/*.cxx )
foreach( testsourcefile ${APP_SOURCES} )
# I used a simple string replace, to cut off .cpp.
string( REPLACE ".cpp" "" testname ${testsourcefile} )
add_executable( ${testname} ${testsourcefile} )
# Make sure YourLib is linked to each app
target_link_libraries( ${testname} YourLib )
endforeach( testsourcefile ${APP_SOURCES} )
Run Code Online (Sandbox Code Playgroud)
file( GLOB )通常不推荐使用,因为如果添加新文件,CMake将不会自动重建.我在这里使用它,因为我不知道你的源文件.find( GLOB ... ). string( REPLACE ... ).我本可以使用带标志的get_filename_componentNAME_WE.关于"一般"CMake信息,我建议您阅读stackoverflow上已经提到的一些广泛的"CMake概述"问题.例如:
当我组织一个包含多个示例文件的 OpenGL 项目时,我发现自己处于类似的情况,其中每个文件都包含一个 main 方法。
\n下面的设置将为每个 c/cpp 文件生成一个单独的可执行文件,并将所需的依赖项复制到目标 bin 文件夹。
\n文件夹结构
\nmy-project\n\xe2\x94\x82\xe2\x94\x80\xe2\x94\x80 ch01_01.c\n\xe2\x94\x82\xe2\x94\x80\xe2\x94\x80 ch02_01.cpp\n\xe2\x94\x82\xe2\x94\x80\xe2\x94\x80 CMakeLists.txt\n\xe2\x94\x82\xe2\x94\x80\xe2\x94\x80 Resources\n\xe2\x94\x82 \xe2\x94\x82\xe2\x94\x80\xe2\x94\x80 Libraries\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82\xe2\x94\x80\xe2\x94\x80 glew\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82\xe2\x94\x80\xe2\x94\x80 bin\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82\xe2\x94\x80\xe2\x94\x80 include\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82\xe2\x94\x80\xe2\x94\x80 lib\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82\xe2\x94\x80\xe2\x94\x80 glfw \n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82\xe2\x94\x80\xe2\x94\x80 include \n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x82\xe2\x94\x80\xe2\x94\x80 lib \nRun Code Online (Sandbox Code Playgroud)\nCMakeLists.txt
\ncmake_minimum_required (VERSION 3.9)\n\nproject ("my-project")\n\ninclude_directories(Resources/Libraries/glew/include\n Resources/Libraries/glfw/include)\n\nlink_directories(Resources/Libraries/glew/lib\n Resources/Libraries/glfw/lib)\n\nlink_libraries(opengl32.lib\n glew32.lib\n glfw3.lib)\n\nset(CMAKE_EXE_LINKER_FLAGS "/NODEFAULTLIB:MSVCRT")\n\nfile(GLOB SOURCE_FILES *.c *.cpp)\n\nforeach(SOURCE_PATH ${SOURCE_FILES})\n\n get_filename_component(EXECUTABLE_NAME ${SOURCE_PATH} NAME_WE)\n\n add_executable(${EXECUTABLE_NAME} ${SOURCE_PATH})\n\n # Copy required DLLs to the target folder\n add_custom_command(TARGET ${EXECUTABLE_NAME} POST_BUILD\n COMMAND ${CMAKE_COMMAND} -E copy_if_different "${CMAKE_SOURCE_DIR}/Resources/Libraries/glew/bin/glew32.dll" \n "${CMAKE_BINARY_DIR}/glew32.dll")\n\nendforeach(SOURCE_PATH ${SOURCE_FILES})\nRun Code Online (Sandbox Code Playgroud)\n可选步骤
\n在视觉工作室中
\n使用“开始”窗口中的“打开本地文件夹”选项打开项目
\n添加新文件时,您可以:
\nadd_executable到CMakeLists.txtTools > Options > CMake由于新添加的文件不会自动获取,因为 CMakeLists.txt 从未更改,因此只需重新生成缓存,如下所示:
\nProject > CMake Cache (x64-Debug) > Delete CacheProject > Generate Cache for my-project现在,您只需右键单击给定的 c/cpp 文件Set as Startup Item即可使用F5.
环境
\n入门模板
\n我在 GitHub 上整理了这个入门模板,以防您感兴趣。
\n| 归档时间: |
|
| 查看次数: |
43114 次 |
| 最近记录: |