Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make CMakelists.txt to include some *.c and *.h files only for one OS?

I want to include some *.c and *.h files only for Windows OS. But I can't find the way how to do it without creating another target, what entails a mistake

I want to do something like this:

add_executable(${TARGET}
     main.cpp
     mainwindow.cpp
     mainwindow.h
     mainwindow.ui
if (WIN32)
     test.c
     test.h
endif()
)

Is there some way to do this?

like image 488
AlexSmth Avatar asked Sep 02 '25 16:09

AlexSmth


1 Answers

The modern CMake solution is to use target_sources.

# common sources
add_executable(${TARGET}
     main.cpp
     mainwindow.cpp
     mainwindow.h
     mainwindow.ui
)

# Stuff only for WIN32
if (WIN32)
    target_sources(${TARGET}
        PRIVATE test.c
        PUBLIC test.h
    )
endif()

This should make your CMakeLists.txt files easier to maintain than wrangling variables.

like image 79
Stephen Newell Avatar answered Sep 05 '25 05:09

Stephen Newell