Xcode can't find or include SDL2 - c++

I have recently downloaded SDL2 and am trying to install it. I created a small test program consisting of a .cpp and a .h file.
I have put SDL2.frameworks inside of /Library/Frameworks and then made sure that it was added to General->Frameworks and Libraries and tried to #include <SDL2/SDL.h>. file not found.
I made sure to go to the build settings->Search Paths->Framework Search Paths to add /Library/Frameworks (it was already listed next to the setting but I readded it to the dropdown). I also added /Library/Frameworks/SDL2.framework to the Header Search Paths. file not found.
I tried to include <SDL2/SDL.h>, <SDL/SDL.h>, <SDL.h>, "SDL2/SDL.h", "SDL/SDL.h" and "SDL.h". none of these files were found.

For the header search path, it should be set to FRAMEWORK_DIRCTORY/SDL2.framework/Headers/, or you can use FRAMEWORK_DIRCTORY/SDL2.framework and turn on recursive search.
Then you should be able to use it with just #include <SDL.h>.
Edit:
After some investigation, seems like my original answer was more of a workaround. The proper way of including SDL library in your code should be using just #include <SDL2/SDL.h>, however it wasn't working.
The reason of that is when compiling the code, Xcode would attempt to copy the library to the product folder, so the compiled executable can have easy access to it. However, for some reason, Xcode copied the framework without the header folder (the reason is probably that Xcode is moving towards using Swift only, which doesn't really have a "header" thing).
By default, when you try to run the code, and it sees there is a SDL2.framework folder located in the product folder, it would use that framework, even if it doesn't have the header folder located in it. But since it doesn't actually have the header folder in it, it doesn't actually run.
To solve it, the easiest way is to remove it from the Targets/Build Phases/Embed Frameworks.
By default when you add a framework to your project, it would also be added here. By removing it from the Embed Frameworks, it won't copy the framework to product folder. And it will only attempt to search SDL framework from whatever you have put in the Build Settings/Framework Search Path.
In the same time, you won't need to have anything for Build Settings/Header Search Path, as it will look for Headers folders in your frameworks first.
And with you code, you can just use #include <SDL2/SDL.h>.
Even better, you can add the framework to Build Phases/Copy Files, and set the Destination to Frameworks, empty the Subpath, and potentially uncheck Copy only when installing, for better cross device usabilities.

Related

How to include SFML source code into my visual studio 2017 c++ project and compile

I have been programming a game in c++ using the sfml library. However, I would like to adjust some of the code of that library, and use that altered code in my project.
So instead of linking the dll I would like to add the source code and then play with that source code. (e.g. for speed optimization).
I know that doing something like that is generally speaking a bad idea. Howeover, I want to learn by playing around a bit and trying different things.
So how would I add the sfml source code to my c++ project in MS visual studio. Note that I am a total noob. I already tried adding the sfml folder that I downloaded from git in the project properties page called "Additional Include Directories", but i am getting errors, of the form "Cannot open include file: 'SFML/Graphics/GLCheck.hpp': No such file or directory" so I guess that i have not yet done enough.
If you want to modify the source code in the library, all you'd have to do is just navigate to where you have SFML installed and go into the code files with a text editor and edit them.
Then, you could link the library to your VS project the same way you would normally but that library is now modified by you.
Seeing as you have a search directory issue already in VS, you must fix that first. Fix that and then go and modify the library's .hpp, .h, .cpp, whatever files in-place.
To fix the search issue.... I don't use VS for graphics, I use CodeBlocks so I am not sure about their GUI to link libraries and change search directories... but, find out where you installed SFML. Check your /usr/include/, it's probably there. Specify that path in the search directories. Just go and find where that GLCheck.hpp file is located. For Example: Say it's full path is /usr/include/SFML/Graphics/GLCheck.hpp... then /usr/include/SFML/Graphics/ or just /usr/include/ (VS might handle it recursively) needs to be in the list of SEARCH DIRECTORIES.

g++/Eclipse wont find include file although path is set

been trying all day to get this to work. I have the AMD APP SDK to do some parallel computing with OpenCL. I set the include path, the library path within my project in Eclipse will keep telling me that the CL.hpp is missing although I can even see it in the includes folder and I can right click on it in the project and see the source code. What is wrong? Even when I try to compile with g++ manually and set the include path with -I it wont work.
I assume you already have the C++ include path for Eclipse set to /opt/AMDAPPSDK-2.9-1/include folder (or a project's relative one which corresponds to that one).
Make sure to use forward slashes unix-style (backward ones aren't guaranteed to work correctly) for your include directive
#include <CL/cl.hpp>

Is there a way to add include directory from C++ code?

I have a project whose vcxproj file is auto generated prior of compilation by using a script during the build process.
this project dependes on a boost library which is installed in a known location on the build machine.
the project header file declares:
#include "boost/foreach.hpp"
which forces me to manually add the path to the boost root folder to the Additional Include Directories field in the vcxproj file.
As the project file is auto-generated it forces me to split the build process into two stages and edit the project file in between.
i have also tried to change the source file and add the full path in the #include statement:
#include "<path to boost root>/boost/foreach.hpp"
but then some boost internal include fails. Which means i can't proceed in this way.
I have read through Set #include directory from C++ code file to find there is no option to add the path from code.
As I'm now on VS2012/C++11 environment i wonder if anything changed in VS2012 / C++11?
Is it still impossible to add an Include directory using a code statement?
You mean dynamically? No, there is no way. The reason is simple:
When you are running your program, it's already compiled, hence the compiler has to know about all the files to include at compile time.
It seems you're using CMake. If that's the case, I recommend you to add the include dirs in the CMake file.
Firstly, doing this:
#include "<path to boost root>/boost/foreach.hpp"
should be strongly discouraged. By doing that, you're making your source code build-able only on your environment - as your project grows, it will be a nightmare to change the path, or for other developers to build it. And, as you discovered, you'll break any headers which are included further down the chain, which are using relative paths.
What kind of script is generating your project? Is it a custom one, or is it a well known build tool such as SCons or CMake? The correct solution is to fix your build script so that it generates the project with the additional include paths correctly.

Trying to figure out Xcode directory system

I'm a bit stuck trying to get box2D to compile and I think it's because I don't quite understand how Xcode handles its build directories.
Box2D is folder containing a set of header and source files (in various subdirectories, etc). I've added the Box2D folder to a coco touch static library project in Xcode and when I try to compile I get errors about header files not being found such as <Box2D/Common/b2BlockAllocator.h>.
I found that if I simply include the header with #include "b2BlockAllocator.h" it compiles fine and Xcode actually finds the file.
So I'm a bit stuck here, I'm assuming I need to find a way to get all includes to begin searching from the root project directory and not from the source files location but I'm not sure how I can do that in Xcode...
Any ideas?
<Box2D/Common/b2BlockAllocator.h> should be found in a directory called Box2D/Common. You said that Box2D contains a set of header and source files. Are some of these headers in a subdirectory of Box2D called Common, or are they all directly under Box2D? If it's the latter, then that's your problem.
OK I figured it out!
In the targets settings page look for 'header search paths' and add the root build directory of your project to it.

setup include path in eclipse CDT in mac os (headers from framework)

for example I need to include a header
#include <OpenGL/glext.h>
while it is actually a header file glext.h under OpenGL.framework/Headers/.
Therefore it is no way to give eclipse a physical path about "OpenGL/glext.h", and I always get unresolved inclusion warning.
I can still build and run them (with managed makefile project) but it is impossible to browse the functions or definitions from those "resolved" header files.
is there any solution?
While not ideal, my solution to this has been to create a folder "/Developer/Framework Headers" (though the name isn't important), and link from e.g. "/System/Library/Frameworks/Foo.framework/Headers" to "Foo" under said folder. Then in Eclipse I add "/Developer/Framework Headers" to a project's includes.
I say not ideal because apart from having to create links for the frameworks you need and add an include to each project, Eclipse seems to have trouble in certain cases such as nested frameworks (e.g. CoreGraphics beneath ApplicationServices), but YMMV.
Yes there is one. Right click on the project in "Project Explore" window and click on the "property". In there Open "C/C++ General" column and choose "Path and symbols". Now you see all of the include library path for this project. If your using C then add the OpenGL library into GNU C, or if you use C++ then add it into GNU C++. Unfortunately you have to do this for every new project. I have been searching for a while how to do this by default but nothing really useful. Hope this help you get rid of those annoying yellow wave lines.
Since current Eclipse CDT releases don't perform sub-framework header inclusion correctly, you can avoid sub-framework problems (like those generated by the CoreServices header files) by creating symbolic links to the include directories of each sub-framework. I elaborated on this subject, which stems from danhan answer on this question, in this blog post.
In order to automate this process, I've created a Z shell script which automates this process and creates the symlink to the specified frameworks' header directory, together with the links to the include directory of each one of their sub-frameworks. The script can be found in this GitHub repository.
Hope this helps.