0

I have a graphics project that requires a 2d editor and a 3d editor. I have a different main.cpp file for each one containing the code to run the program. How do I get the two different executables to be made when I run cmake .. and then make? Do I need to add something to the CMakeLists.txt file?

This is the CMakeLists.txt file that I have:

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})
3
  • Make two add_executable() calls. Commented Mar 2, 2020 at 6:42
  • So, add_executable(${PROJECT_NAME}_bin ${SOURCES}) add_executable(${PROJECT_NAME}_bin2 ${SOURCES}) Or no need to rename the last "bin"? Commented Mar 2, 2020 at 6:45
  • The first parameter of add_executable is also the name of the file to create, so yes. You will probably want a different set of source files, too, otherwise you end up with the same program twice ;) Commented Mar 2, 2020 at 8:25

1 Answer 1

1

As commented, if you want two executables, you should call the add_executable() command twice. Since you have two main.cpp files, you can call add_executable() for each one, while still adding your other remaining source files to both executables.

Also, the use of the file(GLOB ...) command is not recommended, because CMake cannot track changes to your set of source files. Instead, you should list out your source files individually. Your CMake could look something like this:

set(SOURCES
    ${CMAKE_CURRENT_SOURCE_DIR}/src/MyClass1.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/src/MyClass2.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/src/MyHelperFunctions.cpp
)

# Call add_executable() for each main.cpp file.
add_executable(${PROJECT_NAME}_bin_2d 
    ${SOURCES}
    main_2d.cpp
)
add_executable(${PROJECT_NAME}_bin_3d 
    ${SOURCES}
    main_3d.cpp
)
target_link_libraries(${PROJECT_NAME}_bin_2d ${LIBRARIES})
target_link_libraries(${PROJECT_NAME}_bin_3d ${LIBRARIES})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.