Include path using clang++ and SDL - c++

I have an SDL program that I need to compile on my Mac with the line:
#include <SDL.h>
I have libsdl installed using MacPorts, and the output of sdl-config --cflags is:
-I/opt/local/include/SDL -D_GNU_SOURCE=1 -D_THREAD_SAFE
which is, indeed, where the SDL headers are located:
$ ls /opt/local/include/SDL
SDL.h SDL_cpuinfo.h SDL_keyboard.h SDL_rwops.h
...
and then my Makefile invokes the compiler (clang++) with:
CXXFLAGS=`sdl-config --cflags`
Invoking the compiler with -v shows that it is being included in the search path:
#include "..." search starts here:
#include <...> search starts here:
/opt/local/include/SDL
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1
/usr/local/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.1.0/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/usr/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
End of search list.
but the compiler errors with:
./drawable.h:4:10: fatal error: 'SDL.h' file not found
#include <SDL.h>
^
It compiles fine if I change it to #include <SDL/SDL.h>, but other (Linux) machines that the program needs to compile on don't specify it that way. What am I not understanding about how to set up my include path?
EDIT:
Here's a minimal example:
$ cat main.cpp
#include <SDL.h>
int main(int argc, char *argv[]) { }
$ sdl-config --cflags
-I/opt/local/include/SDL -D_GNU_SOURCE=1 -D_THREAD_SAFE
$ ls /opt/local/include/SDL/SDL.h
/opt/local/include/SDL/SDL.h
$ clang++ -v `sdl-config --cflags` main.cpp
...
#include "..." search starts here:
#include <...> search starts here:
/opt/local/include/SDL
....
main.cpp:1:10: fatal error: 'SDL.h' file not found
#include <SDL.h>
^
1 error generated.
ANSWER:
SDL.h turned out to be a broken symbolic link, my own fault.

I think in your Makefile, CXXFLAGS are not included in the compilation command.
Your Makefile should have something like:
$(CXX) $(CXXFLAGS) $(SRC) -o $(EXE)

OK, this just turned out to be my own stupidity, but maybe someone else will benefit from it. I created the /opt/local/include/SDL directory and filled it with soft links to the actual header files in /Library/Frameworks/SDL.framework/Headers, because my project relied on the former. I then, in working on another means of installing SDL, deleted the framework, leaving a bunch of dead links. ls without arguments helpfully showed me filenames, but not that they were links, which I had forgotten by this point. So, SDL.h didn't really exist. A smarter approach might have been to soft link the directory, which would have probably given a more helpful error message, but I was trying to put some other SDL package links in there too.

Related

C++ compiler "no such file or directory" even with include flag set

I have never used a package manager for C++ packages until today. I finally got one to semi work, called cget. I'm trying to use a package called nlohmann/json and from their documentation I just need to run cget install nlohmann/json. The issue is that this puts the include header files in the directory /cget/include/nlohmann which (I assume) the g++ compiler doesn't look in for headers. So I manually moved the nlohmann directory into the /usr/include directory. When I compile with g++ test.cpp -o test it fails with the error "No such file of directory". I clearly see the file in the include path, so what am I missing?
// test.cpp
#include <nlohmann/json.hpp>
I've tried using the Include flag g++ -I /usr/include test.cpp -o test and it still fails.
The path to the json.hpp file is /usr/include/nlohmann/json.hpp
When I run `g++ -print-prog-name=cc1plus` -v it gives:
#include "..." search starts here:
#include <...> search starts here:
/usr/include/c++/9
/usr/include/x86_64-linux-gnu/c++/9
/usr/include/c++/9/backward
/usr/lib/gcc/x86_64-linux-gnu/9/include
/usr/local/include
/usr/include
End of search list.
What is causing this error to persist?

Getting error: fatal error: curl/curl.h: No such file or directory #include <curl/curl.h>

I'm trying to use cURL but every time I try to compile a project with it, I get the same error mentioned in the title. I am aware of the dozen or so post about similar issues. However, most of the solutions I've read so far either don't work or I can't seem to find an analog for me as I'm using mingw32 on a windows 10 OS.
I am trying to use the curl-7.76.1-win32-mingw.zip. I got it from the cURL site, and no matter where I try to stick the files, I can't get anything to compile correctly.
Someone, please explain your solution or ideas to me like I'm 5. My brain has melted.
Here is the actual error:
PS C:\Users\Me> g++ -I "C:\Program Files\Curl\curl-7.76.1-win32-mingw\include\curl" -c "D:\Personal\Projects\#####\#####\#####\#####\main.cpp" -o "D:\Personal\Projects\#####\#####\#####\#####/main"
In file included fromD:\Personal\Projects\#####\#####\#####\#####\main.cpp:4:
D:\Personal\Projects\#####\#####\#####\#####\testapi.hpp:7:10: fatal error: curl/curl.h: No such file or directory
#include <curl/curl.h>
^~~~~~~~~~~~~
compilation terminated.
[Finished in 0.6s]"
Mingw is gcc for windows. Practically everything that applies to gcc applies to mingw.
It's better to use relative paths in your Makefile and with gnu make (which is part of mingw/msys) always use '/' as path separator. If you have to use absolute path like C:\dev\projects\mylib use it this way: C:/dev/projects/mylib.
Back to your curl: curl/curl.h is in C:\path\to\curl-7.76.1-win32-mingw\include\curl\curl.h so you need to add include directory to your gcc command line that points to the right directory, which is C:\path\to\curl-7.76.1-win32-mingw\include because you use "curl/curl.h". If you don't have it (that curl) installed system wide it's also better to use "curl/curl.h" in #include path than <curl/curl.h>.
So all you have to do is add -Ipath/to/curl-7.76.1-win32-mingw/include to your compile line, like:
g++ -O2 -Ipath/to/curl-7.76.1-win32-mingw/include -c -omain.o main.cpp
It can be done automatically in Makefile:
CXX = g++
CFLAGS += -O2
INCLUDES += -Ipath/to/curl-7.76.1-win32-mingw/include
.cpp.o:
$(CXX) -c $(CXXFLAGS) $(INCLUDES) $*.cpp
You write:
g++ -I "C:\Program Files\Curl\curl-7.76.1-win32-mingw\include\curl"
try better:
g++ -I "C:\Program Files\Curl\curl-7.76.1-win32-mingw\include"
as you have written in the code sample:
#include <curl/curl.h>
or conserve the -I option as it was, and change your #include to:
#include <curl.h>
I sincerely hope this will help.

SDL2_image not found

I am trying to compile the following code which has the headers:
#include <SDL2/SDL.h>
#include <SDL2_image/SDL_image.h>
However after running the following makefile:
g++ -std=c++11 src/main.cpp -lSDL2 -lSDL2_image
I get the following error:
fatal error: SDL2_image/SDL_image.h: No such file or directory
#include <SDL2_image/SDL_image.h>
Any suggestions? Not entirely sure about my installation of SDL_image. I am running this on Ubuntu.
This problem can be solved through installing libsdl2-image-dev package:
apt install libsdl2-image-dev
Run apt-file search SDL_image.h
The result will tell you the location of the include file.
For instance, /usr/include/SDL2/SDL_image.h was returned.
So, when you want to include SDL_image.h, write everything after the include/ in between < >.
Thus, includes should look like the following:
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
See the question's comments for the original discussion regarding this solution.
From SDL documentation, it says that add 'lSDL_image' to the end of the compile line.
cc -o myprogram mysource.o `sdl-config --libs` -lSDL_image
or
gcc -o myprogram mysource.c `sdl-config --libs` -lSDL_image
Here is the reference -> https://www.libsdl.org/projects/docs/SDL_image/SDL_image.html
Section 2.2 Compiling.
So for SDL2, you just need to change 'lSDL_image' to 'lSDL2_image'.
For Windows + SDL2-2.0.8 + SDL_image-2.0.4 + Codeblocks you've got the add both Runtime Binaries and Development Libraries to the compiler and linker. Or else, you'll get the error SDL2_image not found, even with having the dll in your program's directory, this occurs. Hopefully others find this helpful; I had to figure it out myself. Example: If your resources are separate, you'll be adding the two plus your standard SDL2 paths to your compiler and linker. Warning: SDL2_image.h has it's headers assuming that the headers are in the same folder as the SDL2 framework. If you get errors about the image header, include the sub-folder SDL2 from SDL framework in the path and then you should be including SDL2 in the program as: include <SDL.h> rather than include <SDL2/SDL.h>.

mlton gives library-related error

When I try to compile a program with mlton, I get an error.
~/projects/serve-sml $ mlton server.mlb
In file included from /usr/lib/mlton/include/platform.h:13:0,
from /usr/lib/mlton/include/common-main.h:16,
from /usr/lib/mlton/include/amd64-main.h:11,
from /tmp/file86PWQJ.1.c:110:
/usr/lib/mlton/include/cenv.h:137:17: fatal error: gmp.h: No such file or directory
#include "gmp.h"
^
compilation terminated.
call to system failed with exit status 1:
gcc -std=gnu99 -c -I/usr/lib/mlton/targets/self/include -I/usr/lib/mlton/include -O1 -fno-common -fno-strict-aliasing -fomit-frame-pointer -w -m64 -o /tmp/fileg5D5To.o /tmp/file86PWQJ.1.c
~/projects/serve-sml $
According to this, that means I should install libgmp3-dev. I've run apt-get install libgmp3-dev, and verified that the file /usr/include/x86_64-linux-gnu/gmp.h exists, but am still getting the same error.
Any idea what I'm doing wrong?
Try executing gcc -v -x c -E - in your terminal. This will print out the header file search path that your C compiler is using. I get something like:
#include "..." search starts here:
#include <...> search starts here:
/Users/ml9951/include
.
/usr/local/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.1.0/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/usr/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
So if I had gmp.h in /usr/include/x86_64-linux-gnu/, then my C compiler would not find it. I would suggest moving your .h file into a directory that is on this search path (like /usr/local/include in my case), or passing in the -I /usr/include/x86_64-linux-gnu/ flag, which would require modifying MLton's build scripts. Chances are you are going to have to do the same thing with libgmp.a when it comes time to link

How to query the default include paths of clang++?

How can I query the default include path of clang/clang++? I am trying to use a custom built clang compiler (the one that supports OpenMP), but it doesn't seem to find the STL libraries:
/usr/local/bin/clang++ hello.cpp
hello.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
^
1 error generated.
By using an IDE, back-tracking the #include iostream, and finally using the -isystem option I got the simple helloworld application to compile in OSX 10.9:
/usr/local/bin/clang++ -isystem /Library/Developer/CommandLineTools/usr/lib/c++/v1 hello.cpp
Thanks for your help!
You are looking for option -v. Compiling with clang++ -c file.cc -v will print among other things:
#include "..." search starts here:
#include <...> search starts here:
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9
etc.
If you run
clang++ -### hello.cpp
It will display the commands used to compile that particular file, including the default include paths, library search paths, targets etc.