CMake Static library's dependency can't be found












1















I'm building a static library and linking it into my executable target. The library builds fine in a vacuum, but when I try to include a header from the library, I get a "No such file" error pointing at the static library's dependency.



My understanding is that my static lib should privately include its dependencies, and the consumer shouldn't have to do anything besides linking the library. Is this wrong? Or am I just including the static lib's dependencies improperly?



CMakeLists.txt for the static lib:



# Find SDL2 and associated libs
find_package(SDL2 REQUIRED)
find_package(SDL2_image REQUIRED)
find_package(SDL2_mixer REQUIRED)
find_package(SDL2_ttf REQUIRED)

# Build static library dependency SDL2pp
set(SDL2PP_WITH_IMAGE ON)
set(SDL2PP_WITH_MIXER ON)
set(SDL2PP_WITH_TTF ON)
add_subdirectory("${PROJECT_SOURCE_DIR}/../Libraries/libSDL2pp/"
"${CMAKE_CURRENT_BINARY_DIR}/Libraries/libSDL2pp/")


# Add our static library target
add_library(Framework_Game STATIC
Private/GameManager.cpp
Public/GameManager.h
)

target_include_directories(Framework_Game
PRIVATE
${SDL2_INCLUDE_DIRS} ${SDL2_IMAGE_INCLUDE_DIRS}
${SDL2_MIXER_INCLUDE_DIRS} ${SDL2_TTF_INCLUDE_DIRS}
${SDL2PP_INCLUDE_DIRS}
${CMAKE_CURRENT_SOURCE_DIR}/Private
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/Public
)

target_link_libraries(Framework_Game
PRIVATE
${SDL2_LIBRARIES} ${SDL2_IMAGE_LIBRARIES}
${SDL2_MIXER_INCLUDE_DIRS} ${SDL2_TTF_INCLUDE_DIRS}
${SDL2PP_LIBRARIES}
)


CMakeLists.txt for the executable:



add_executable(NewWorlds Launch.cpp)

target_link_libraries(NewWorlds
PRIVATE
Framework_Game
)


GameManager.h includes SDL2pp with #include <SDL2pp/SDL2pp.hh. I'm then including it in Launch.cpp with #include <GameManager.h>, which gives the error SDL2pp/SDL2pp.hh: No such file or directory










share|improve this question



























    1















    I'm building a static library and linking it into my executable target. The library builds fine in a vacuum, but when I try to include a header from the library, I get a "No such file" error pointing at the static library's dependency.



    My understanding is that my static lib should privately include its dependencies, and the consumer shouldn't have to do anything besides linking the library. Is this wrong? Or am I just including the static lib's dependencies improperly?



    CMakeLists.txt for the static lib:



    # Find SDL2 and associated libs
    find_package(SDL2 REQUIRED)
    find_package(SDL2_image REQUIRED)
    find_package(SDL2_mixer REQUIRED)
    find_package(SDL2_ttf REQUIRED)

    # Build static library dependency SDL2pp
    set(SDL2PP_WITH_IMAGE ON)
    set(SDL2PP_WITH_MIXER ON)
    set(SDL2PP_WITH_TTF ON)
    add_subdirectory("${PROJECT_SOURCE_DIR}/../Libraries/libSDL2pp/"
    "${CMAKE_CURRENT_BINARY_DIR}/Libraries/libSDL2pp/")


    # Add our static library target
    add_library(Framework_Game STATIC
    Private/GameManager.cpp
    Public/GameManager.h
    )

    target_include_directories(Framework_Game
    PRIVATE
    ${SDL2_INCLUDE_DIRS} ${SDL2_IMAGE_INCLUDE_DIRS}
    ${SDL2_MIXER_INCLUDE_DIRS} ${SDL2_TTF_INCLUDE_DIRS}
    ${SDL2PP_INCLUDE_DIRS}
    ${CMAKE_CURRENT_SOURCE_DIR}/Private
    PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}/Public
    )

    target_link_libraries(Framework_Game
    PRIVATE
    ${SDL2_LIBRARIES} ${SDL2_IMAGE_LIBRARIES}
    ${SDL2_MIXER_INCLUDE_DIRS} ${SDL2_TTF_INCLUDE_DIRS}
    ${SDL2PP_LIBRARIES}
    )


    CMakeLists.txt for the executable:



    add_executable(NewWorlds Launch.cpp)

    target_link_libraries(NewWorlds
    PRIVATE
    Framework_Game
    )


    GameManager.h includes SDL2pp with #include <SDL2pp/SDL2pp.hh. I'm then including it in Launch.cpp with #include <GameManager.h>, which gives the error SDL2pp/SDL2pp.hh: No such file or directory










    share|improve this question

























      1












      1








      1








      I'm building a static library and linking it into my executable target. The library builds fine in a vacuum, but when I try to include a header from the library, I get a "No such file" error pointing at the static library's dependency.



      My understanding is that my static lib should privately include its dependencies, and the consumer shouldn't have to do anything besides linking the library. Is this wrong? Or am I just including the static lib's dependencies improperly?



      CMakeLists.txt for the static lib:



      # Find SDL2 and associated libs
      find_package(SDL2 REQUIRED)
      find_package(SDL2_image REQUIRED)
      find_package(SDL2_mixer REQUIRED)
      find_package(SDL2_ttf REQUIRED)

      # Build static library dependency SDL2pp
      set(SDL2PP_WITH_IMAGE ON)
      set(SDL2PP_WITH_MIXER ON)
      set(SDL2PP_WITH_TTF ON)
      add_subdirectory("${PROJECT_SOURCE_DIR}/../Libraries/libSDL2pp/"
      "${CMAKE_CURRENT_BINARY_DIR}/Libraries/libSDL2pp/")


      # Add our static library target
      add_library(Framework_Game STATIC
      Private/GameManager.cpp
      Public/GameManager.h
      )

      target_include_directories(Framework_Game
      PRIVATE
      ${SDL2_INCLUDE_DIRS} ${SDL2_IMAGE_INCLUDE_DIRS}
      ${SDL2_MIXER_INCLUDE_DIRS} ${SDL2_TTF_INCLUDE_DIRS}
      ${SDL2PP_INCLUDE_DIRS}
      ${CMAKE_CURRENT_SOURCE_DIR}/Private
      PUBLIC
      ${CMAKE_CURRENT_SOURCE_DIR}/Public
      )

      target_link_libraries(Framework_Game
      PRIVATE
      ${SDL2_LIBRARIES} ${SDL2_IMAGE_LIBRARIES}
      ${SDL2_MIXER_INCLUDE_DIRS} ${SDL2_TTF_INCLUDE_DIRS}
      ${SDL2PP_LIBRARIES}
      )


      CMakeLists.txt for the executable:



      add_executable(NewWorlds Launch.cpp)

      target_link_libraries(NewWorlds
      PRIVATE
      Framework_Game
      )


      GameManager.h includes SDL2pp with #include <SDL2pp/SDL2pp.hh. I'm then including it in Launch.cpp with #include <GameManager.h>, which gives the error SDL2pp/SDL2pp.hh: No such file or directory










      share|improve this question














      I'm building a static library and linking it into my executable target. The library builds fine in a vacuum, but when I try to include a header from the library, I get a "No such file" error pointing at the static library's dependency.



      My understanding is that my static lib should privately include its dependencies, and the consumer shouldn't have to do anything besides linking the library. Is this wrong? Or am I just including the static lib's dependencies improperly?



      CMakeLists.txt for the static lib:



      # Find SDL2 and associated libs
      find_package(SDL2 REQUIRED)
      find_package(SDL2_image REQUIRED)
      find_package(SDL2_mixer REQUIRED)
      find_package(SDL2_ttf REQUIRED)

      # Build static library dependency SDL2pp
      set(SDL2PP_WITH_IMAGE ON)
      set(SDL2PP_WITH_MIXER ON)
      set(SDL2PP_WITH_TTF ON)
      add_subdirectory("${PROJECT_SOURCE_DIR}/../Libraries/libSDL2pp/"
      "${CMAKE_CURRENT_BINARY_DIR}/Libraries/libSDL2pp/")


      # Add our static library target
      add_library(Framework_Game STATIC
      Private/GameManager.cpp
      Public/GameManager.h
      )

      target_include_directories(Framework_Game
      PRIVATE
      ${SDL2_INCLUDE_DIRS} ${SDL2_IMAGE_INCLUDE_DIRS}
      ${SDL2_MIXER_INCLUDE_DIRS} ${SDL2_TTF_INCLUDE_DIRS}
      ${SDL2PP_INCLUDE_DIRS}
      ${CMAKE_CURRENT_SOURCE_DIR}/Private
      PUBLIC
      ${CMAKE_CURRENT_SOURCE_DIR}/Public
      )

      target_link_libraries(Framework_Game
      PRIVATE
      ${SDL2_LIBRARIES} ${SDL2_IMAGE_LIBRARIES}
      ${SDL2_MIXER_INCLUDE_DIRS} ${SDL2_TTF_INCLUDE_DIRS}
      ${SDL2PP_LIBRARIES}
      )


      CMakeLists.txt for the executable:



      add_executable(NewWorlds Launch.cpp)

      target_link_libraries(NewWorlds
      PRIVATE
      Framework_Game
      )


      GameManager.h includes SDL2pp with #include <SDL2pp/SDL2pp.hh. I'm then including it in Launch.cpp with #include <GameManager.h>, which gives the error SDL2pp/SDL2pp.hh: No such file or directory







      c++ cmake






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 1 at 19:30









      fang273fang273

      185




      185
























          2 Answers
          2






          active

          oldest

          votes


















          2














          You made the SDL2pp dependency private for Framework_Game thus it doesn't propagate to targets depending on Framework_Game. I don't know the SDL2pp library so I can't tell exactly which of these need to be public, but



          target_include_directories(Framework_Game
          PRIVATE
          ${CMAKE_CURRENT_SOURCE_DIR}/Private
          PUBLIC
          ${SDL2_INCLUDE_DIRS} ${SDL2_IMAGE_INCLUDE_DIRS}
          ${SDL2_MIXER_INCLUDE_DIRS} ${SDL2_TTF_INCLUDE_DIRS}
          ${SDL2PP_INCLUDE_DIRS}
          ${CMAKE_CURRENT_SOURCE_DIR}/Public
          )

          target_link_libraries(Framework_Game
          PUBLIC
          ${SDL2_LIBRARIES} ${SDL2_IMAGE_LIBRARIES}
          ${SDL2_MIXER_INCLUDE_DIRS} ${SDL2_TTF_INCLUDE_DIRS}
          ${SDL2PP_LIBRARIES}
          )


          will definitely work.






          share|improve this answer































            1















            My understanding is that my static lib should privately include its dependencies, and the consumer shouldn't have to do anything besides linking the library. Is this wrong? Or am I just including the static lib's dependencies improperly?




            This is a design decision, but yes, that's usually the good practice. But it means that no SDL header must be included in the public headers of the library! That's your job. Otherwise, you need to propagate the dependency, as Corristo said.






            share|improve this answer
























            • Using either approach, I get past the original issue but run into SDL2pp getting undefined reference errors as it tries to call SDL functions. Is there something I'm missing in supplying SDL2pp with the SDL dependencies? This is a new issue, SDL2pp was working fine before I moved this code out to a library.

              – fang273
              Jan 1 at 20:31








            • 1





              Got it to work by changing SDL2pp from passing old-style variables to Modern target-based linking.

              – fang273
              Jan 1 at 21:39











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998330%2fcmake-static-librarys-dependency-cant-be-found%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            You made the SDL2pp dependency private for Framework_Game thus it doesn't propagate to targets depending on Framework_Game. I don't know the SDL2pp library so I can't tell exactly which of these need to be public, but



            target_include_directories(Framework_Game
            PRIVATE
            ${CMAKE_CURRENT_SOURCE_DIR}/Private
            PUBLIC
            ${SDL2_INCLUDE_DIRS} ${SDL2_IMAGE_INCLUDE_DIRS}
            ${SDL2_MIXER_INCLUDE_DIRS} ${SDL2_TTF_INCLUDE_DIRS}
            ${SDL2PP_INCLUDE_DIRS}
            ${CMAKE_CURRENT_SOURCE_DIR}/Public
            )

            target_link_libraries(Framework_Game
            PUBLIC
            ${SDL2_LIBRARIES} ${SDL2_IMAGE_LIBRARIES}
            ${SDL2_MIXER_INCLUDE_DIRS} ${SDL2_TTF_INCLUDE_DIRS}
            ${SDL2PP_LIBRARIES}
            )


            will definitely work.






            share|improve this answer




























              2














              You made the SDL2pp dependency private for Framework_Game thus it doesn't propagate to targets depending on Framework_Game. I don't know the SDL2pp library so I can't tell exactly which of these need to be public, but



              target_include_directories(Framework_Game
              PRIVATE
              ${CMAKE_CURRENT_SOURCE_DIR}/Private
              PUBLIC
              ${SDL2_INCLUDE_DIRS} ${SDL2_IMAGE_INCLUDE_DIRS}
              ${SDL2_MIXER_INCLUDE_DIRS} ${SDL2_TTF_INCLUDE_DIRS}
              ${SDL2PP_INCLUDE_DIRS}
              ${CMAKE_CURRENT_SOURCE_DIR}/Public
              )

              target_link_libraries(Framework_Game
              PUBLIC
              ${SDL2_LIBRARIES} ${SDL2_IMAGE_LIBRARIES}
              ${SDL2_MIXER_INCLUDE_DIRS} ${SDL2_TTF_INCLUDE_DIRS}
              ${SDL2PP_LIBRARIES}
              )


              will definitely work.






              share|improve this answer


























                2












                2








                2







                You made the SDL2pp dependency private for Framework_Game thus it doesn't propagate to targets depending on Framework_Game. I don't know the SDL2pp library so I can't tell exactly which of these need to be public, but



                target_include_directories(Framework_Game
                PRIVATE
                ${CMAKE_CURRENT_SOURCE_DIR}/Private
                PUBLIC
                ${SDL2_INCLUDE_DIRS} ${SDL2_IMAGE_INCLUDE_DIRS}
                ${SDL2_MIXER_INCLUDE_DIRS} ${SDL2_TTF_INCLUDE_DIRS}
                ${SDL2PP_INCLUDE_DIRS}
                ${CMAKE_CURRENT_SOURCE_DIR}/Public
                )

                target_link_libraries(Framework_Game
                PUBLIC
                ${SDL2_LIBRARIES} ${SDL2_IMAGE_LIBRARIES}
                ${SDL2_MIXER_INCLUDE_DIRS} ${SDL2_TTF_INCLUDE_DIRS}
                ${SDL2PP_LIBRARIES}
                )


                will definitely work.






                share|improve this answer













                You made the SDL2pp dependency private for Framework_Game thus it doesn't propagate to targets depending on Framework_Game. I don't know the SDL2pp library so I can't tell exactly which of these need to be public, but



                target_include_directories(Framework_Game
                PRIVATE
                ${CMAKE_CURRENT_SOURCE_DIR}/Private
                PUBLIC
                ${SDL2_INCLUDE_DIRS} ${SDL2_IMAGE_INCLUDE_DIRS}
                ${SDL2_MIXER_INCLUDE_DIRS} ${SDL2_TTF_INCLUDE_DIRS}
                ${SDL2PP_INCLUDE_DIRS}
                ${CMAKE_CURRENT_SOURCE_DIR}/Public
                )

                target_link_libraries(Framework_Game
                PUBLIC
                ${SDL2_LIBRARIES} ${SDL2_IMAGE_LIBRARIES}
                ${SDL2_MIXER_INCLUDE_DIRS} ${SDL2_TTF_INCLUDE_DIRS}
                ${SDL2PP_LIBRARIES}
                )


                will definitely work.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 1 at 19:37









                CorristoCorristo

                2,437925




                2,437925

























                    1















                    My understanding is that my static lib should privately include its dependencies, and the consumer shouldn't have to do anything besides linking the library. Is this wrong? Or am I just including the static lib's dependencies improperly?




                    This is a design decision, but yes, that's usually the good practice. But it means that no SDL header must be included in the public headers of the library! That's your job. Otherwise, you need to propagate the dependency, as Corristo said.






                    share|improve this answer
























                    • Using either approach, I get past the original issue but run into SDL2pp getting undefined reference errors as it tries to call SDL functions. Is there something I'm missing in supplying SDL2pp with the SDL dependencies? This is a new issue, SDL2pp was working fine before I moved this code out to a library.

                      – fang273
                      Jan 1 at 20:31








                    • 1





                      Got it to work by changing SDL2pp from passing old-style variables to Modern target-based linking.

                      – fang273
                      Jan 1 at 21:39
















                    1















                    My understanding is that my static lib should privately include its dependencies, and the consumer shouldn't have to do anything besides linking the library. Is this wrong? Or am I just including the static lib's dependencies improperly?




                    This is a design decision, but yes, that's usually the good practice. But it means that no SDL header must be included in the public headers of the library! That's your job. Otherwise, you need to propagate the dependency, as Corristo said.






                    share|improve this answer
























                    • Using either approach, I get past the original issue but run into SDL2pp getting undefined reference errors as it tries to call SDL functions. Is there something I'm missing in supplying SDL2pp with the SDL dependencies? This is a new issue, SDL2pp was working fine before I moved this code out to a library.

                      – fang273
                      Jan 1 at 20:31








                    • 1





                      Got it to work by changing SDL2pp from passing old-style variables to Modern target-based linking.

                      – fang273
                      Jan 1 at 21:39














                    1












                    1








                    1








                    My understanding is that my static lib should privately include its dependencies, and the consumer shouldn't have to do anything besides linking the library. Is this wrong? Or am I just including the static lib's dependencies improperly?




                    This is a design decision, but yes, that's usually the good practice. But it means that no SDL header must be included in the public headers of the library! That's your job. Otherwise, you need to propagate the dependency, as Corristo said.






                    share|improve this answer














                    My understanding is that my static lib should privately include its dependencies, and the consumer shouldn't have to do anything besides linking the library. Is this wrong? Or am I just including the static lib's dependencies improperly?




                    This is a design decision, but yes, that's usually the good practice. But it means that no SDL header must be included in the public headers of the library! That's your job. Otherwise, you need to propagate the dependency, as Corristo said.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 1 at 19:40









                    Matthieu BrucherMatthieu Brucher

                    16k32141




                    16k32141













                    • Using either approach, I get past the original issue but run into SDL2pp getting undefined reference errors as it tries to call SDL functions. Is there something I'm missing in supplying SDL2pp with the SDL dependencies? This is a new issue, SDL2pp was working fine before I moved this code out to a library.

                      – fang273
                      Jan 1 at 20:31








                    • 1





                      Got it to work by changing SDL2pp from passing old-style variables to Modern target-based linking.

                      – fang273
                      Jan 1 at 21:39



















                    • Using either approach, I get past the original issue but run into SDL2pp getting undefined reference errors as it tries to call SDL functions. Is there something I'm missing in supplying SDL2pp with the SDL dependencies? This is a new issue, SDL2pp was working fine before I moved this code out to a library.

                      – fang273
                      Jan 1 at 20:31








                    • 1





                      Got it to work by changing SDL2pp from passing old-style variables to Modern target-based linking.

                      – fang273
                      Jan 1 at 21:39

















                    Using either approach, I get past the original issue but run into SDL2pp getting undefined reference errors as it tries to call SDL functions. Is there something I'm missing in supplying SDL2pp with the SDL dependencies? This is a new issue, SDL2pp was working fine before I moved this code out to a library.

                    – fang273
                    Jan 1 at 20:31







                    Using either approach, I get past the original issue but run into SDL2pp getting undefined reference errors as it tries to call SDL functions. Is there something I'm missing in supplying SDL2pp with the SDL dependencies? This is a new issue, SDL2pp was working fine before I moved this code out to a library.

                    – fang273
                    Jan 1 at 20:31






                    1




                    1





                    Got it to work by changing SDL2pp from passing old-style variables to Modern target-based linking.

                    – fang273
                    Jan 1 at 21:39





                    Got it to work by changing SDL2pp from passing old-style variables to Modern target-based linking.

                    – fang273
                    Jan 1 at 21:39


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998330%2fcmake-static-librarys-dependency-cant-be-found%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas