CLion and CMake: only building a library without an executable?
Asked Answered
M

2

16

How to only build a static library with clion without having an executable? How does the CMakeLists.txt look like? (without add_executable)

Update: If I don't add executable to Clion, I have an error, that an executable is required.

Here my CMakeLists.txt.

Matronna answered 30/3, 2015 at 20:45 Comment(4)
Your problem is unrelated to CLion, it is only a CMake question.Flyblow
Ok, then your problem is completely unrelated to CMake. You configure to run an executable and you don't have an executable, just a library. CLion cannot execute a library, thus the error.Flyblow
I misunderstood your question. Does this help? https://mcmap.net/q/749433/-clion-build-depend-targetsFlyblow
CMakeLists.txt is not available anymore, I have checked web.archive.org.Lifeline
S
21

This is an old question. But I'll add the answer to your question as a help for other people. You need to replace your add_executable with add_library

add_library(target_name source_files)
Sporocyst answered 27/5, 2015 at 4:38 Comment(1)
And if you want to build a dynamic library, you can use add_library(target_name SHARED source_files).Avocado
R
4

Short answer: compile the library target and run any custom command as a placeholder to avoid the warning.

Long answer:

CLion lets you either build the whole project and run executables/configurations.

When running executables, you can choose a target to compile and the executable to run after compiling the target. For executable targets, they are usually the same thing. You compile the executable target and then run it.

It seems like this is not really designed for libraries, but there is a workaround. You usually set the target to be the library and the executable to be any executable that depends on this library. However, this is usually not very useful because if you wanted to run this executable you could just set this executable to be the target and the library would be built anyway.

So people probably want to just build the library (to check if everything is OK, etc) without compiling or running any extra executable targets. In this case, the workaround is to choose a custom executable to run after building the library, and the error message is gone.

You have nothing useful you need to run after building the library, just choose any cheap command as a placeholder. Something like "pwd" or "ls".

Setting up a configuration

Rosenkrantz answered 21/12, 2020 at 7:57 Comment(2)
how do you create a pwd executable?Accrete
You can just type "pwd" or anything in that box.Rosenkrantz

© 2022 - 2024 — McMap. All rights reserved.