CMake Static library's dependency can't be found
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
add a comment |
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
add a comment |
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
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
c++ cmake
asked Jan 1 at 19:30
fang273fang273
185
185
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
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.
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Jan 1 at 19:37
CorristoCorristo
2,437925
2,437925
add a comment |
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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