4 ms·
CMake works fine if you keep things as simple as possible which often means going against best practices. For instance, this is my template for a C project usin
by krapp 4y ago
CMake works fine if you keep things as simple as possible which often means going against best practices. For instance, this is my template for a C project using SDL, keeping all of my code and libs in their own local hierarchy:
set(PROJECT "ProjectTemplate") #this can't have spaces
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_BUILD_TYPE "Debug")
set(ARCH "x64")
set(PROJECT_ROOT "${CMAKE_CURRENT_LIST_DIR}/project")
set(PROJECT_SRC "${PROJECT_ROOT}/src")
set(PROJECT_INC "${PROJECT_ROOT}/include")
set(PROJECT_ASSETS "${PROJECT_ROOT}/assets")
set(PROJECT_LIBS "${PROJECT_ROOT}/libs") #third party libraries
set(SDL2_PATH "${PROJECT_LIBS}/SDL")
project(${PROJECT} C)
add_executable(${PROJECT} "${PROJECT_SRC}/main.c")
set(PROJECT_SOURCE_FILES "${PROJECT_SRC}/main.c")
target_sources(${PROJECT} PUBLIC ${PROJECT_SOURCE_FILES})
set(SDL2_INCLUDE_DIR "${SDL2_PATH}/include")
target_include_directories(${PROJECT} PUBLIC ${PROJECT_INC}
${SDL2_INCLUDE_DIR})
set(SDL2_LIBRARY "${SDL2_PATH}/lib/${ARCH}")
target_link_libraries(${PROJECT} "${SDL2_LIBRARY}/SDL2.lib"
"${SDL2_LIBRARY}/SDL2main.lib")
file(GLOB_RECURSE PROJECT_DLLS "${SDL2_LIBRARY}/SDL2.dll")
add_custom_command(TARGET ${PROJECT} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different ${PROJECT_DLLS}
"$<TARGET_FILE_DIR:${PROJECT}>")
if(EXISTS ${PROJECT_ASSETS})
add_custom_command(TARGET ${PROJECT} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory ${PROJECT_ASSETS}
"$<TARGET_FILE_DIR:${PROJECT}>/assets")
endif()
Is this the way it's supposed to be done? No. Is it setting someone's teeth on edge? Probably. Is it cross-platform compatible? No, but I don't need it to be, I just need it to build for the specific platform I'm using, which is Windows. Will it build? Yes, and that's all that matters.
- palata 4y agoI would argue that if the best practices don't work fine, maybe you are not actually following the best practices. But I agree with the "keep things simple" (e.g. don't define custom functions), but I don't think that your snippet does that (ahem, I see `add_custom_command` there) :)
- krapp 4y ago>(ahem, I see add_custom_command there) :) It's worth it when you're working on assets or lua scripts and want to import any changes for each rebuild.
- deterministic 4y agoThat’s what I do as well. You can do pretty much all you need to do with add_custom_command and add_custom_target. I use it for Typescript, Java etc. and it works fine.