2

In my CMakeLists.txt I want to create a binary named foo and a library named libfoo(.a|.so).

So I have tried using the following code:

add_executable(foo ${BIN_SRC})
add_library(foo ${LIB_SRC})

But unfortunately the code above fails with the error message:

Make Error at CMakeLists.txt:156 (add_library):
add_library cannot create target "foo" because another target with
the same name already exists.  The existing target is an executable created

It is abovious that I have 2 targets named foo in my CMakeLists.txt which is causing the error. I could e.g. fix the bug using:

    add_library(libfoo ${LIB_SRC})

But then my library gets named liblibfoo(.a|.so) which is not what I want!?

1 Answer 1

3

Name of the executable/library file isn't forced to be the same as the target. You may adjust name of the file created using OUTPUT_NAME target's property:

add_library(libfoo ${LIB_SRC})
set_target_properties(libfoo PROPERTIES OUTPUT_NAME foo)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that's exactly what I was looking for!
So, please accept the answer to indicate it's solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.