[仅使用cmake时如何使用2个不同的main.cpp文件制作2个不同的可执行文件?

问题描述 投票:0回答:1

我有一个图形项目,需要2d编辑器和3d编辑器。我每个人都有一个不同的main.cpp文件,其中包含运行程序的代码。运行cmake ..然后执行make时,如何获取2个不同的可执行文件?我需要在cmake.txt文件中添加一些内容吗?

这是我拥有的cmake.txt文件:

cmake_minimum_required(VERSION 2.8.12)
project(Assignment2)

### Output directories
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")

### Compilation flags: adapt to your needs ###
if(MSVC)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP /bigobj") ### Enable parallel compilation
  set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR} )
  set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR} )
else()
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
endif()

### Add src to the include directories
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/src")

### Include Eigen for linear algebra
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/ext/eigen")

### Compile GLFW3 statically
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL " " FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL " " FORCE)
set(GLFW_BUILD_DOCS OFF CACHE BOOL " " FORCE)
set(GLFW_BUILD_INSTALL OFF CACHE BOOL " " FORCE)
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/ext/glfw" "glfw")
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/ext/glfw/include")
set(LIBRARIES "glfw" ${GLFW_LIBRARIES})

### On windows, you also need glew
if((UNIX AND NOT APPLE) OR WIN32)
  set(GLEW_INSTALL OFF CACHE BOOL " " FORCE)
  add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/ext/glew" "glew")
  include_directories("${CMAKE_CURRENT_SOURCE_DIR}/ext/glew/include")
  list(APPEND LIBRARIES "glew")
endif()

### Compile all the cpp files in src
file(GLOB SOURCES
"${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp"
)

add_executable(${PROJECT_NAME}_bin ${SOURCES})
target_link_libraries(${PROJECT_NAME}_bin ${LIBRARIES})
c++ makefile cmake
1个回答
1
投票

如前所述,如果需要两个可执行文件,则应调用add_executable()命令两次。由于您有两个main.cpp文件,因此您可以为每个文件调用add_executable(),同时仍将其他剩余的源文件添加到both可执行文件中。

此外,不建议使用add_executable()命令,因为CMake无法跟踪对源文件集的更改。相反,您应该单独列出源文件。您的CMake可能看起来像这样:

file(GLOB ...)
© www.soinside.com 2019 - 2024. All rights reserved.