C++ Cmake (Add Non-Built Files)

C++ CMake (add non-built files)

You should be able to just add them to your list of sources in whichever add_executable or add_library call is appropriate and they will appear in the IDE.

I believe CMake uses the files' extensions to determine if they are actual source files, so if yours have extensions like ".txt" or ".log" they won't be compiled.

Cmake add directory without source files

CMake doesn't care whether a directory contains source files or something else. A directory added by add_subdirectory() must contain a CMakeLists.txt file, but that's the only requirement. What the code in that subdirectory's CMakeList does is entirely up to you.

How to set cmake, in order to add txt files into working directory as resource?

You can use file(COPY idiom:

file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/test.txt
DESTINATION ${CMAKE_CURRENT_BINARY_DIR})

But may I also suggest configure_file with the COPYONLY option. In this way, when test.txt is modified, CMake will reconfigure and regenerate the build. If you don't need that, just use file(COPY

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/test.txt
${CMAKE_CURRENT_BINARY_DIR} COPYONLY)

You will also see many people using add_custom_command to copy files, but that is more useful when you must copy a file in between build steps:

add_custom_command(
TARGET untitled POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_SOURCE_DIR}/test.txt
${CMAKE_CURRENT_BINARY_DIR}/test.txt)

I think in your case, the first example is most appropriate, but now you have tools in your toolbox for all scenarios.

How to add a 'non-built' file to a project with C-Make

If the file is generated by something CMake understands (that is, by a custom command in the same CMakeList), you can just list it and CMake will pick up the dependency by itself. For other cases, there is a source file property GENERATED which you can set on the source file to indicate it will be generated during build:

add_executable(${PROJECT_NAME}
main.cpp
.
.
.
${MOCSrcs}
moc_X.cxx ?
)

set_property(SOURCE moc_X.cxx PROPERTY GENERATED TRUE)

CMake: Is it possible to build an executable from only static libraries and no source?

There is no way to do it without a hack. You need at least one *.c or *.cpp file.

What I do is make a dummy null.cpp file (zero bytes) and use that. You can also use /dev/null but that only works on Linux.

file(WRITE null.cpp "")

add_executable(tester
null.cpp
)

target_link_libraries(tester
-Wl,--whole-archive
libtest1
libtest2
libtest3
libtest4
-Wl,--no-whole-archive
gtest_main
)

Creating a library in CMake depending on source files not available when generating build files

Personally, I dislike the ExternalProject_Add command, because it does way too many things for my taste, but I've digressed.

What if you do something like this, where bar is simulating your ExtendedThirdPartyLib target, since it depends on generated files

cmake_minimum_required(VERSION 3.11)

project(lol C)

set(SOURCES lol.c) # only this file exists

add_library(lol ${SOURCES})

set(FOO_FILES "foo1.c" "foo2.c")

add_custom_command(OUTPUT ${FOO_FILES}
COMMAND ${CMAKE_COMMAND} -E touch ${FOO_FILES}
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
COMMENT "Creating ${FOO_FILES}"
VERBATIM)

add_custom_target(foo DEPENDS ${FOO_FILES})

add_library(bar ${FOO_FILES})

add_dependencies(bar foo)

target_link_libraries(lol bar)

The whole approach hinges on the fact that the method, where produced/generated files are procured, is explicitly defined via the custom command and associated custom target.

You should modify the custom command to extract the required files (e.g. could even call some external script) from the tarball (which might require downloading with curl or something similar).

C++/C Importing Third Party Library to CMake

In general you have two approaches -

  1. Install cglm library & then make use of it in your project
  2. Build cglm as part of your project

I have used both approaches & found the latter to be far better. Especially for smaller projects for these reasons -

  • Better intellisense, you can jump to 3rd party code and even edit
  • Easy to package and ship the project artifacts
  • Easy to manage in CI, version upgrades fo 3rd party projects

I use FectchContent CMake api to achieve this. (Alternatively same can be achieved by adding third party source-code to your project manually too)

Now I have not worked on cglm personally, but still a sample build file

cmake_minimum_required(VERSION 3.16)

project("MathPlease")

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

include(FetchContent)
set(FETCHCONTENT_QUIET FALSE)

fetchcontent_declare(
cglm
GIT_REPOSITORY https://github.com/recp/cglm.git
GIT_TAG v0.8.5
GIT_PROGRESS TRUE
)

if(NOT cglm_POPULATED)
message("populating cglm")
fetchcontent_populate(cglm)
add_subdirectory(${cglm_SOURCE_DIR} ${cglm_BUILD_DIR})
endif()

add_executable(${PROJECT_NAME} MathPlease.cpp)

target_link_libraries(${PROJECT_NAME} cglm)

P.S. FetchContent is a fairly recent CMake feature. You will need CMake > 3.11



Related Topics



Leave a reply



Submit