Search code examples
c++preprocessorpremake

Preprocessor definitions not carried across premake includes


I have 3 projects in premake: Project A (static library):

project "A"
    kind "StaticLib"
    language "C++"
    staticruntime "on"

    files
    {
        "src/**.h",
        "src/**.cpp",
        
        "include/**.h"
    }

    includedirs
    {
        "include"
    }

    defines
    {
        "STATIC_DEFINE"
    }

In the dll.h file, inside Project A I have:

#ifdef STATIC_DEFINE
#pragma message("Static Define")
#else 
#pragma message("No Static Define")
#endif

The other two projects are:

project "B"
    kind "StaticLib"
    language "C++"
    staticruntime "on"
    
    files
    {
        some project files, one of them includes dll.h
    }

    links
    {
        "A"
    }
Project "C"
    kind "ConsoleApp"
    language "C++"
    staticruntime "on"

    files
    {
        again, project files, one includes dll.h
    }

    links
    {
        "B"
    }

there is one final premake file which includes them all

workspace "Workspace"
    architecture "x86_64"
    startproject "C"

include "libs/A"
include "libs/B"
include "C"

This is not the actual code in my premake files, they are kind of bloated and I judged pseudocode would be enough to explain the problem and fix it, if necessary I can edit the question and add the full premake files and project structure.

What I would expect is for C to have STATIC_DEFINE defined, since it includes B which includes A which defines the macro, but the output is as follows:

Build started(A):
Static Define
Build started(B):
No Static Define
Build started(C)
No Static Define

Again, this is not the full output since it is a full application which has quite a few files, and most of it is not relevant to the question, but I can provide it if needed

I tried adding the

defines 
{
    "STATIC_DEFINE"
}

to Project B, and then it appeared in the output for that Project B but not for Project C only when I add the definition to all 3 premake files does it work

Any help to do this without adding the definitions to the premake files would be appreciated, thank you.


Solution

  • Your

    defines { "STATIC_DEFINE" }
    

    is at project "A" scope and so only applies to project "A".

    There are not (yet?) usage to "inherit" from some properties (includedir, links, defines).

    You might create Lua function for that:

    function UseA()
        defines { "STATIC_DEFINE" }
        includedirs { "include" }
        links {"A"}
    end
    

    and then

    project "B"
        kind "StaticLib"
        language "C++"
        staticruntime "on"
        
        files
        {
            -- some project files
        }
    
        useA()