0

I would like to first declare an target, say, an static library, then feeding its source files according to some options. Is it possible to achieve that?

Say, what I want is a command target_specify_sources(), and the full script is like:

cmake_minimum_required(VERSION 3.17)
project(hello)
option(USE_PLUGIN "use plugin?" ON)

add_library(my_target STATIC)

if (USE_PLUGIN)
    target_specify_sources(my_target 
        src/base.cpp
        src/plugin.cpp
    )
else()
    target_specify_sources(my_target 
        src/base.cpp
    )
endif()


update:

I shoud make clear that "to declare a target, means not specifying any source files". The comment mentioned another question link:

Can one add further source files to an executable once defined?

there is target_sources() command since cmake 3.1, but it is not "declare target".

ChrisZZ
  • 1,521
  • 2
  • 17
  • 24
  • 1
    https://stackoverflow.com/questions/9339851/can-one-add-further-source-files-to-an-executable-once-defined ? – KamilCuk Jul 07 '21 at 15:40
  • 2
    In your example you could just add `src/base.cpp` to your add_library() and add the additional source for the plugin optionally using `target_sources()` – drescherjm Jul 07 '21 at 15:40
  • @KamilCuk Oh thanks for mentions that question. I never know `target_sources()` until you and drescherjm mentioned. – ChrisZZ Jul 08 '21 at 10:25
  • @drescherjm But I still have to specify at least one source file when defining my target. I just feel that this is not really "modern cmake" that "target oriented". – ChrisZZ Jul 08 '21 at 10:26

0 Answers0