0

This might be a really dumb question. I'm using my CMakeLists.txt which is declare source files in this way(I know it isn't a good practice):

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src DIR_SRC)
add_executable(hello "${DIR_SRC}")

After watch this speaking. I know that maybe I should not use include_directories. But will it be lengthy and unreadable if I specify all source files manually when I call add_executable?

BTW, if I have some classes need to call different API on different platform. Should I use

if (WIN32)
    add_executable(hello SomeClassWin32.cpp SomeClass.h)
elseif (UNIX)
    add_executable(hello SomeClassLinux.cpp SomeClass.h)
endif ()

or just use #ifdef.

Any help will be really appreciated.

StephanXu
  • 49
  • 5
  • The developers say yes, in that globbing is not recommended. See the note in the middle of this page: [https://cmake.org/cmake/help/v3.16/command/file.html#glob](https://cmake.org/cmake/help/v3.16/command/file.html#glob) – drescherjm Jan 28 '20 at 18:24
  • 2
    How would your compiler know what files to build if you don't tell it about all of them? Magic? – Jesper Juhl Jan 28 '20 at 18:25
  • 1
    If you don't want to specify every file, you can use `file(GLOB ...`, yet CMake doesn't recommend this. – paxbun Jan 28 '20 at 18:27
  • 1
    @JesperJuhl Presumably by scanning a working directories and discriminating on file extension, or something like that. – François Andrieux Jan 28 '20 at 18:28
  • 6
    As a person who has used `CMake` for 12 years now at work, specifying each file is not really an issue for me even for projects with a thousand source files and hundreds of thousands of source code lines. The reason is its not like you ever need to add 100 source files in a single day! With that said for simple example projects / development of concepts projects I do use globbing of folders. – drescherjm Jan 28 '20 at 18:32
  • @drescherjm So in your project you just `add_executable(hello source1.cpp source1.h source2.cpp source2.h ...)` (maybe a single file in a line?) – StephanXu Jan 29 '20 at 05:37
  • 2
    I usually use `CMake` variables for the source files and headers. I usually set these variables in the same `CMake` script. As for the format of how I set the variables I usually use one line per file with the closing ) on its own line. That way I comment out a line to remove a file from the list. At the end of the list if there is a folder involved in each path I have some lines with just the path commented out to make it easy to add additional entries. – drescherjm Jan 29 '20 at 12:11
  • @drescherjm What about to use `target_source` in `CMakeLists.txt` which is inside each sub folder. I'm inspired by [this](https://crascit.com/2016/01/31/enhanced-source-file-handling-with-target_sources/). BTW should I also add all of my header files in `target_source` at the same time?(the article's author above has mentioned this) – StephanXu Jan 29 '20 at 13:15
  • I am still doing things the old way. I have not used target_sources() yet. One reason is I have projects from 2008 that are still in production. – drescherjm Jan 29 '20 at 13:32
  • You may want to ask a completely new question about target_sources() and your concerns about the usage – drescherjm Jan 29 '20 at 13:35
  • @drescherjm Thanks a lot. – StephanXu Jan 29 '20 at 16:57

0 Answers0