Program3
├── CMakeLists.txt
├── CMakePresets.json
├── CPPs
├── Program3.cpp // this is example code i'm trying to run
├── Program3.txt // this is the premade .cpp file that came with the cmake template
├── Hs
├── Program3.h //this is the premade .h file that came with the cmake template
├── out
I'm using the CMAKE template in visual studio with C++ and want to run an SFML example code. For the code to run I need to include .hpp files. I dont want to copy the include files and paste them directly into my project, and am hoping to just reference the library that is in it's own directory.
this is a tutorial i'm following but slightly altering https://www.youtube.com/watch?v=c35L674qzIE
i'm unsure of the syntax but I think i'm supposed to use target_include_directories or include_directories to have the program search for the file it needs in the SFML folder.
I may be mistaken and just copy files into the project as suggested if referencing them from outside the project isn't possible.
the error is
Severity Code Description Project File Line Suppression State Details
Error C1083 Cannot open include file: 'SFML/Graphics.hpp': No such file or directory C:\Users\saled\OneDrive\Desktop\GameDev\StrategieLLC\1stAttempt\Program3\out\build\x64-debug\Program3 C:\Users\saled\OneDrive\Desktop\GameDev\StrategieLLC\1stAttempt\Program3\CPPs\Program3.cpp 1
target_include_directories({CMAKE_SOURCE_DIR} ../DevLibrariesUsed/SFML-2.6.1-windows-vc17-64-bit/SFML-2.6.1/include/SFML)
the .. references the common folder between the project and library folder DevLibariesUsed.
i've also tried
target_include_directories(Program3 ../DevLibrariesUsed/SFML-2.6.1-windows-vc17-64-bit/SFML-2.6.1/include/SFML)
and
include_directories(Program3 ../DevLibrariesUsed/SFML-2.6.1-windows-vc17-64-bit/SFML-2.6.1/include/SFML)
But I may try just linking each .hpp file, but there are more folders in thaat part of the library and I don't want to have to add everything in those directly at some point.
this
target_link_libraries(Program3 ../DevLibrariesUsed/SFML-2.6.1-windows-vc17-64-bit/SFML-2.6.1/lib/sfml-window.lib)
seems to work for the .lib files? So i don't know why similar syntax doesn't work for include_directories
Your problem seems to be the lack of knowledge about what include_directories, target_include_directory and target_link_libraries does.
include_directoriesdoes not do what you think it does. When you useinclude_directories, you're telling CMake that in this folder there is anotherCMakeLists.txtto continue the definition of the project.From the information you have given us,
target_include_directoriesdefinitiely is what you want to use.target_include_directoriesis used to tell the compiler where your source-code is located, and gives it scope to said directory.target_link_librariesis a complete different story. Withtarget_link_libraries, as the name suggest, you link a library to your project. For this you need to decide if you want/need it to be static or dynamic. If you are new to the concept of libraries, you might want to read up a bit on what static and dynamic actually means. In your case, it is probably a static library.To conclude, what you probably want to do is:
target_include_directoriesto enable scope to said source-code.target_link_librariesANDtarget_include_directoriesto link the library to your project.Generally, you should probably read through the documentation of CMake and learn some fundamentals before trying to follow a Youtube video, as 273K suggested in the comments.