c++premake

Linking static library fails with Premake5


I'm using Premake5 as the build system for a C++ project, which currently consists of a static library (Core) and a console application (Tests). However, when I attempt to compile Tests including files from Core, the compiler produces the following error:

/mnt/HD/Linux/Jetbrains/CLion/bin/cmake/linux/x64/bin/cmake --build /home/renerp/Documentos/Projetos/C++/Chess/.premake/generated/cmake-build-debug --target Tests -j 3
[1/15] Building CXX object CMakeFiles/Tests.dir/home/renerp/Documentos/Projetos/C++/Chess/tests/unit/core/pieces/PawnTest.cpp.o
FAILED: CMakeFiles/Tests.dir/home/renerp/Documentos/Projetos/C++/Chess/tests/unit/core/pieces/PawnTest.cpp.o 
/usr/bin/g++ -DDEBUG -I/home/renerp/Documentos/Projetos/C++/Chess/tests/vendor/gtest -I/home/renerp/Documentos/Projetos/C++/Chess/tests/vendor/gtest/include -std=c++17 -fdiagnostics-color=always -g -std=c++17 -MD -MT CMakeFiles/Tests.dir/home/renerp/Documentos/Projetos/C++/Chess/tests/unit/core/pieces/PawnTest.cpp.o -MF CMakeFiles/Tests.dir/home/renerp/Documentos/Projetos/C++/Chess/tests/unit/core/pieces/PawnTest.cpp.o.d -o CMakeFiles/Tests.dir/home/renerp/Documentos/Projetos/C++/Chess/tests/unit/core/pieces/PawnTest.cpp.o -c /home/renerp/Documentos/Projetos/C++/Chess/tests/unit/core/pieces/PawnTest.cpp
/home/renerp/Documentos/Projetos/C++/Chess/tests/unit/core/pieces/PawnTest.cpp:5:10: fatal error: Piece.hpp: Arquivo ou diretório inexistente
    5 | #include <Piece.hpp>
      |          ^~~~~~~~~~~
compilation terminated.
[3/15] Building CXX object CMakeFiles/Tests.dir/home/renerp/Documentos/Projetos/C++/Chess/tests/unit/core/utils/coordinates/Coordinates.cpp.o
ninja: build stopped: subcommand failed.

I'm using the Jarod cmake module, which is why the error involves CMake.

Using the default gmak2 gives the following error:

==== Building Core (debug) ====
Creating ../build/Debug/obj/Core
...
==== Building Tests (debug) ====
Creating ../build/Debug/obj/Tests
Coordinates.cpp
MainTest.cpp
PawnTest.cpp
../../tests/unit/core/pieces/PawnTest.cpp:5:10: fatal error: Piece.hpp: Arquivo ou diretório inexistente
    5 | #include <Piece.hpp>
      |          ^~~~~~~~~~~
compilation terminated.
make[1]: *** [Tests.make:167: ../build/Debug/obj/Tests/PawnTest.o] Erro 1
make: *** [Makefile:38: Tests] Erro 2

The current premake file content is the following:

workspace "Chess"
    configurations { "Debug", "Release" }

    cppdialect "C++17"

    location ".premake/generated/"
    bindirs ".premake/build/%{cfg.buildcfg}/bin/%{prj.name}"
    objdir ".premake/build/%{cfg.buildcfg}/obj/%{prj.name}"

    project "Core"
        kind "StaticLib"
        language "C++"
        
        files { "src/core/**.hpp", "src/core/**.cpp" }
        includedirs { "src/core", "src/core/**" }

        filter "configurations:Debug"
            defines { "DEBUG" }
            symbols "On"
        filter "configurations:Release"
            defines { "NDEBUG" }
            optimize "On"
        filter ""

    project "Tests"
        kind "ConsoleApp"
        language "C++"

        files {
            "tests/unit/**.cpp",
            "tests/MainTest.cpp",
            "tests/vendor/gtest/**.h",
            "tests/vendor/gtest/src/*.cc"
        }
        includedirs {
            "tests/vendor/gtest/",
            "tests/vendor/gtest/include/"
        }
        links { "Core" }

        filter "configurations:Debug"
            defines { "DEBUG" }
            symbols "On"

I expected this configuration to correctly set up the include paths and link the Core library with the Tests application. What might be causing this issue, and how can I resolve it?


Solution

  • You forget to add proper includedirs in "Tests" (links does propagate includedirs):

    project "Tests"
    -- ...
            includedirs {
                "tests/vendor/gtest/",
                "tests/vendor/gtest/include/",
                "src/core", "src/core/**"
            }