0

I have

dir
    CMakeLists.txt
    main.c
    subdir
        CMakeLists.txt
        file1.c
        file2.c

I am trying to build a target like so

dir CMakeLists.txt has

add_subdirectory(subdir)
add_executable(main main.c ${EXTRA_SOURCES})

subdir CMakeLists.txt has

set(EXTRA_SOURCES file1.c file2.c)

I get a linking error for file1.c and file2.c. They are not being compiled into main binary. I have tried switching

adding_executable(main main.c ${EXTRA_SOURCES})
add_subdirectory(subdir)

I still get linking errors. The compiler will give undefined reference to ... because file1.c and file2.c were not added to main.c objects.

How do I properly set cmake to compile files in the sub directory to the main executable in the main directory?

On Demand
  • 65
  • 5
  • Normally, variables defined in the subdirectory are not visible in the main directory. Use `PARENT_SCOPE` option for propagate them. See also other answers to the [duplicate question](https://stackoverflow.com/questions/8934295/add-source-in-a-subdirectory-to-a-cmake-project). – Tsyvarev Jul 19 '20 at 12:25
  • @Tsyvarev thanks I had been trying to use add_sources which wasnt working. I now realise its important to also declare PRIVATE and PUBLIC – On Demand Jul 19 '20 at 12:55

0 Answers0