Skip to content

Instantly share code, notes, and snippets.

@SofijaErkin
Last active March 23, 2022 00:00
Show Gist options
  • Save SofijaErkin/a7db1e2f8cce04ab090f5e3312c00cd5 to your computer and use it in GitHub Desktop.
Save SofijaErkin/a7db1e2f8cce04ab090f5e3312c00cd5 to your computer and use it in GitHub Desktop.
This is a manual for C/C++ project using CMake to write CMakeLists TXTs.

CMakeLists TXTs Manual

1.Architeture

Refer to GitHub Project.

I can remember that the Project architeture just like:

cxx_project

|

|__CMakeLists.txt

|__README.md

|_apps

|    |__CMakeLists.txt

|    |__README.md

|    |_apps_active

|    |    |__CMakeLists.txt

|    |    |__README.md

|    |    |_include

|    |    |    |__call_apps_active.h

|    |    |_source

|    |         |__call_apps_active.cpp

|    |__apps_main.cpp

|_common_external_library

|    |__CMakeLists.txt

|    |__README.md

|    |_include

|    |    |_call_external_lib.h

|    |_source

|         |_call_external_lib.cpp

|_interface_test_bug

     |__CMakeLists.txt

     |__README.md

     |_include

     |    |__pro_test_catch_bug.h

     |_source

          |__pro_release_test_realize.cpp

2.Top CMakeListx TXT

Please see "top_CMakeLists.txt".

3.Apps CMakeLists TXT

Please see "apps_CMakeLists.txt".

4.Common External CMakeLists TXT

Please see "common_external_CMakeLists.txt".

5.Apps Active CMakeLists TXT

Please see "apps_active_CMakeListst.txt".

6.Interface Test bugs CMakeLists TXT

Please see "interface_test_bugs_CMakeLists.txt".

add_library(apps_active "")
target_sources(apps_active
PRIVATE
${CMAKE_CURRENT_LIST_DIR}/source/call_apps_active.cpp
PUBLIC
${CMAKE_CURRENT_LIST_DIR}/include/call_apps_active.h
)
target_include_directories(apps_active
PUBLIC
${CMAKE_CURRENT_LIST_DIR}
)
# using main.cpp to product main.cpp.o, then using main.cpp.o to generate
# target main
add_executable(apps_main main.cpp)
# add sub class/library directory
add_subdirectory(apps_active)
# target file to link sub and apposed class/library directory
target_link_libraries(apps_main
PRIVATE
com_ext_lib_mata
apps_active
)
add_library(com_ext_lib_mata "")
target_sources(com_ext_lib_mata
PRIVATE
${CMAKE_CURRENT_LIST_DIR}/source/call_external_lib.cpp
PUBLIC
${CMAKE_CURRENT_LIST_DIR}/include/call_external_lib.h
)
target_include_directories(com_ext_lib_mata
PUBLIC
${CMAKE_CURRENT_LIST_DIR}
)
# Notice: have changed!
include_directories(${CMAKE_CURRENT_LIST_DIR}/include/*.h)
add_executable(interface_test_mata source/pro_release_test_realize.cpp)
target_link_libraries(pro_test_bugs apps_active)
add_test(
NAME
pro_release_test_apps_active
COMMAND
$<TARGET_FILE:pro_test_bugs>
)
# Reference: https://medium.com/@dexterchan_44737/visual-studio-code-build-and-debug-a-c-with-cmake-on-mac-os-7633bc59bf34
# Reference: <https://stackoverflow.com/a/68360488/10846570>
cmake_minimum_required(VERSION 3.0.0...3.22 FATAL_ERROR)
# CMake Minimum Required Version
project(cxx_project_manual
VERSION
0.1.0
DESCRIPTION
"A project to manual compile, debug using CMake"
LANGUAGES
CXX
)
set(CMAKE_CXX_STANDARD 11)
message(WARNING CMAKE_CXX_STANDARD="${CMAKE_CXX_STANDARD}")
# set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_EXTENSIONS OFF)
message(WARNING CMAKE_CXX_EXTENSIONS="${CMAKE_CXX_EXTENSIONS}")
set(CMAKE_CXX_STANDARD_REQUIRED ON)
message(WARNING CMAKE_CXX_STANDARD_REQUIRED="${CMAKE_CXX_STANDARD_REQUIRED}")
include(GNUInstallDirs)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY
${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
message(WARNING CMAKE_ARCHIVE_OUTPUT_DIRECTORY="${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY
${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
message(WARNING CMAKE_LIBRARY_OUTPUT_DIRECTORY="${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY
${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})
message(WARNING CMAKE_RUNTIME_OUTPUT_DIRECTORY="${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
# Notice: this three command need to be changed for mac
# Control where the static and shared libraries are built so that on windows
# we don't need to tinker with the path to run the executable
# defines targets and sources
add_subdirectory(apps)
# contains an "external" library we will link to
add_subdirectory(common_external_library)
# enable testing and define tests
# using test to catch bugs
include(CTest)
enable_testing()
add_subdirectory(interface_test_bug)
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment