filipdutescu / modern-cpp-template

A template for modern C++ projects using CMake, Clang-Format, CI, unit testing and more, with support for downstream inclusion.
The Unlicense
1.69k stars 214 forks source link

[BUG] cmake fails when Project_BUILD_EXECUTABLE is true #16

Closed jmarca closed 4 years ago

jmarca commented 4 years ago

Describe the bug If I try to build executable by setting -DProject_BUILD_EXECUTABLE=On, the cmake step fails with

CMake Error at test/CMakeLists.txt:51 (target_link_libraries):
  Target "Project" of type EXECUTABLE may not be linked into another target.
  One may link only to INTERFACE, OBJECT, STATIC or SHARED libraries, or to
  executables with the ENABLE_EXPORTS property set.

To Reproduce Steps to reproduce the behavior:

  1. Clone project
  2. mkdir build && cd build
  3. cmake .. -DProject_BUILD_EXECUTABLE=On
  4. See error

Expected behavior I'm just trying to build a simple executable, not a library

Desktop (please complete the following information):

cmake --version cmake version 3.16.3

Additional context Coming back to C++ after forever away, just learning about the build tooling etc, so it could be a basic mistake on my end. Just want to build an executable program not a library. I failed after tweaking things for my project, and then got the same failure in the vanilla template project with no tweaks.

filipdutescu commented 4 years ago

Thanks for the report! There seems to be indeed a bug when making an executable, since the tests are linking against it, as if it were a library. For the moment, you can turn of unit testing (i.e. cmake .. -DProject_BUILD_EXECUTABLE=On -DProject_ENABLE_UNIT_TESTING=0), while I try to figure out a solution which would not eliminate unit testing from the project.

ghost commented 4 years ago

I think this line cause the error. https://github.com/filipdutescu/modern-cpp-template/blob/1974980aced7b43ebdf5462d5cf99156929a6044/CMakeLists.txt#L56 and it's reasonable. There is a tmp.hpp file, and it's implementation file tmp.cpp. These are a library. If we want to use them as an executable, either the tmp.cpp should provide a main file or there must be another file that include main function and that file should be the executable. I think it's better to remove the option at all.

filipdutescu commented 4 years ago

@jmarca fixed the problem in b53aec0. Could you confirm it works for you as well?

jmarca commented 3 years ago

Apologies but I haven’t had time to test this. If I get to it I’ll reopen

James

On Aug 15, 2020, at 02:26, Filip Dutescu notifications@github.com wrote:

 Closed #16 via eea7e38.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.