Hello, hope everyone is doing well. So I’m working on my own C++ library that includes SDL, and I have a separate project that I’m using to test that library. I can build my library just fine, but my test project cannot find SDL.h when I try to build it.
So do I have to also link SDL to every project that uses my library? Or can I link it to my library in a way that it becomes available to any projects that use it?
I’ve searched for answers on Google and on here but searching “How to include a library in my own library” I get nothing relevant. It’s just difficult to explain to a search engine, so any help here would be awesome. I’ve just joined so I hope this is an appropriate topic.
Edit: My library is a static library, in case that’s of importance.
I don’t know what’s going on … You include SDL in the library and that’s it …
I linked my library to SDL already. When I tried to use my library with my test project, the test project was unable to find SDL.h
To get it to work, I had to link SDL to my test project even though it’s already linked to the library it’s using. I just want to make it so that any project that uses my library, can link to my library and not need to be linked to SDL again.
It just seems redundant to link to a library that uses a library, then also have to link to the library that that library is already linked to.
So firstly did you use something like
#include <SDL.h>. If you did, this has nothing to do with linking and instead with finding the header file. For me the
SDL.h file is in
/usr/include/SDL2/SDL.h and it should be added to the include paths for the compiler with
-I /usr/include/SDL2/ since the compiler by default doesn’t search there (
echo | gcc -xc++ -E -Wp,-v - will get the default include paths for c++). This isn’t a linking problem because this is for the preprocessor.
You give only generalities … What exactly did you do and what came out?
Yeah I should have been more specific my bad.
Ok so I’m using Visual Studio 2019 on Windows 11, and I’m working on a game engine that just uses SDL right now. The game engine is a static library named “Engine” that is in it’s own Visual Studio project, separate from “Engine Test” which I’ve created for testing the “Engine” library. I have SDL on my disk at C:\libraries\SDL\SDL2-2.0.20. I’ve gone into project settings in my Engine project, added the Include Directories, Library Directories, and Librarian->Additional Dependencies settings to the appropriate x64 SDL folders “SDL2-2.0.20/include”, “SDL2-2.0.20/lib/x64” and SDL2.lib, SDL2main.lib respectively.
Now in my “Engine Test” project, I added the folder with the .lib file “Engine/Engine/Debug/x64/” in the Engine project to the Include Directories, on the project settings page. On the C/C++ settings page, I added the root “Engine” folder that contains the header files to Additional Include Directories.
At this point, I ran the Engine Test project with Debug configuration and x64 as the platform. The only error it gave me was
“1>C:\Users\zvisg\source\repos\Engine\Engine\DM_Texture.h(3,10): fatal error C1083: Cannot open include file: ‘SDL.h’: No such file or directory”
So at the moment, I’ve added the Include and Library directories to x64 SDL in my Engine Test project as “SDL2-2.0.20/include” and “SDL2-2.0.20/lib/x64” in order to get my Engine Test project to work, and it does work now. What I’m trying to do is make it so that I don’t have to add these SDL “include” and “lib” directories to the Engine Test Project, or any project that uses my library.
If there’s any additional information needed, or if what I’m trying to do can’t be done please let me know. While I have used SDL with C++ for a few years now, I’ve only ever used it directly in the game project. So this is my first time putting my game engine in it’s own library. That way I don’t have to rewrite my whole engine every time I make a game. Thanks for the help ROSY.