1

I'm a CMake-beginner. Here's a problem I cannot figure out. I have a project structure that looks like this:

root
| common/
|   include/
|      public.h
|   src/
|      file-1.c
|      file-2.c
| win-exec/
|    main.c
| pico-exec/
|    main.c

win-exec is a Windows executable binary that depends on code in common/. It also depends on GLFW and a few other Windows-specific libraries.

pico-exec is another executable binary for the Raspberry Pi Pico that depends on code in common/. This also has dependencies on the Pico SDK.

So basically I want different executables for different platforms.

How would you structure this as a CMake-project? The executables both need to compile the common code, since the executables need to be built for different architectures.

Is it possible to define different targets (with seperate toolchains) that somehow also recompiles the common code in the process? Or should I approach this outside of CMake with a batch-file for instance?

r1sc
  • 11
  • 1
  • 1
    Have seperate `CMakeLists.txt` files for the two binaries. Alternatively can have a single CMakeLists.txt with platform conditionals. – kaylum Aug 12 '21 at 07:21
  • @kaylum is right -- those are your two options. I'd recommend the separate projects route. – Alex Reinking Aug 12 '21 at 08:22
  • Add the common code to a static lib. Add a `CMakeLists.txt` file to `common` and be sure that this file does not generate any files to the source tree. This allows you to add the common logic to the separate projects without duplicating the common logic. If desired you could add a toplevel `CMakeLists.txt` file that adds the subdirectory depending on the target platform; something like: `add_subdirectory(common) if (WIN32) add_subdirectory(win-exec) else() add_subdirectory(pico-exe) endif()` – fabian Aug 12 '21 at 18:17
  • Thanks you all for your suggestions! I have a couple of things to try now, thank you! – r1sc Aug 12 '21 at 19:21
  • @fabian Is this even possible without a top-level CMakeLists? How does the binary projects know about the library project? add_subdirectory doesn't seem to like parent directories. – r1sc Aug 12 '21 at 19:39
  • @r1sc you CAN use any directories with `add_subdirectory`, but if the specified directory is not a subdirectory to the current source directory on the file system, you need to specify a second parameter dedicating a binary directory for the directory to use. – fabian Aug 12 '21 at 21:03

0 Answers0