Adding an include path to the parent directory in GCC - c++

I'm wanting to include files from the parent directory in a project I am working on. All of the header files are in the parent directory, is there any way of using -I on the commandline to search for includes in the parent directory without using an absolute path?
I know I can solve these issues using a makefile and I will probably end up doing this, but I'd like to know if there is a quick commandline trick I can use as this situation pops up a lot when I am making quick prototype code.
Currently I am trying to compile using:
g++ -Wall -I../ simple.cpp
but this seems to not work properly. Will I also need to change the includes in simple.cpp from #include include_file.hpp to #include ../include_file.hpp ?

Hmm...
g++ -Wall -I.. simple.cpp
and
// Somewhere in simple.cpp
#include <include_file.hpp>
should work.

Related

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.

Compiling examples from GLFW?

I am trying to compile the file simple.c in glfw-3.2.1/examples on Ubuntu 18.04. I am using the following compilation command:
gcc -o simple simple.c glad.c -lglfw3 -lGL -lm -lXrandr -lXi -lX11 -lXxf86vm -lpthread -ldl -lXinerama -lXcursor
I have copied and pasted glad.c and glad.h into this examples folder, as well as the include folder that came with glad.zip
However, I when I try to compile the code I get the following:
glad.c:25:10: fatal error: glad/glad.h: No such file or directory
#include <glad/glad.h>
I don't understand why this is, since I am including glad.c in the compilation command.
I am following this tutorial to set up glad https://learnopengl.com/Getting-started/Creating-a-window. Unfortunaly, this opengl tutorial is geared towards MS Windows. Is there an easier way to set up glad on Ubuntu (sudo apt install ...)?
What am I missing here?
Thanks
Including glad.c in the compilation command will not bring the header in, it will compile glad.c and bring in the object from the generated file in (so it would be somewhat close to -lglad if you had installed some glad library)
Unfortunately there does not seem to be any ubuntu package for glad, the next simplest thing would be to simply compile the examples along with glfw (just run cmake and make in the glfw folder), but we can fix that anyway.
Understanding the error
glad.c:25:10: fatal error: glad/glad.h: No such file or directory
#include <glad/glad.h>
says "the file glad.c wants a header located in glad/glad.h, but I cannot find it"
This can either be a problem with include paths (gcc is not looking in the directories you intended it to look at), or the file really is not here.
Looking at glad.c and simple.c, they use this syntax:
#include <glad/glad.c
Includes can be of two kinds, either through double-quotes in which case they are called "local includes" and gcc will look for the headers in the current directory, or with angle brackets and they are usually "system includes"; you need to tell gcc where to look for them with the -I option
Fixing it
There are multiple ways to fix that.
We can use a command line/environment the c files expect.
First, respect the zip hierarchy, the post you linked to says there should be two include directories, so you need to put the headers where they were in the zip file (glad.h in the glad directory) ;
then tell gcc to look for include files in the current directory with -I. (. is the current directory)
The command line will then look like something like gcc -o simple simple.c glad.c -I. -lglfw3 -l...
or
change simple.c and glad.c to include "glad.h" instead of <glad/glad.h> ; the files will then look for the file where you had it automatically.
Having tried to compile simple.c the same way you did now, you will also need a linmath.h header; I am not sure if it comes with glad but glad and linmath.h are in the deps directory of glfw in the git tree, I would assume they also are in the tar.

g++ compilation using 'include' statement with non-relative paths (C++)

I am trying to compile a c++ code with a third party library using g++ as a compiler.
My main.cpp needs to include the header file core.hpp while the core.hpp needs to include cvdef.h whereas cvdef.h need to include interface.h.
The paths for these three headers in the include statements are as follows:
#include "opencv2/core.hpp"
#include "opencv2/core/cvdef.h"
#include "opencv2/core/hal/interface.h"
See file structure in image below.
When I compile my main.cpp it finds the core.hpp. The core.hpp, however, cannot seems to find cvdef.h as it is looking within the 'core'-folder for the 'opencv2'-folder (which is a level below).
Without changing the paths in the include statement, how would I go about this?
My current compile statement using g++ under Windows is:
g++ main.cpp -o main
It seems that OpenCV2 wants to look for the header files in standard locations.
You can add to the list of standard locations by using the -I (upper-case i) option, specifying the path to add.
In your case you should be able to do it with
g++ main.cpp -o main -Iopencv2/core

G++ Add include and linking path

My Problem
I read somewhere that to add include paths to g++ you need to use the -I option:
g++ -I /some/directory and_then_my_files.cpp
However, I'm not sure if this is actually what I need. It's currently not working for me, and I couldn't find anything else close to what I have.
My Setup
I've got a directory with all my current project code, and in it a subdirectory classes, that contains various .h and .cpp files containing classes that may or may not be used when compiling my main files.
Since g++ ./classes/*.cpp main.cpp takes a long time (large number of files in classes directory), I'm looking for an alternative that only compiles and links the files that are included in the main file.
Main file:
#include "classes/a.h"
#include "classes/b.h"
// ... my code
And as you can imagine, g++ complains about undefined references to classes A and B, unless I add ./classes/*.cpp to the build command.
What I want to achieve
So -I and -L did not work, and adding the whole directory to the build command results in a ridiculously long build time - I'm talking 3-5 minutes, which really slows down my development speed.
Is there any way to only build/link the included classes from my classes directory, such as only classes A and B from the example I gave above?
Small Recap of what I've already tried
g++ -I ./classes main.cpp -o main
g++ -L ./classes main.cpp -o main (probably stupid but I tried it anyway)
g++ ./classes/*.cpp main.cpp -o main (what I currently have to resort to)
Thanks in advance :)
A temporary workaround
export CPLUS_INCLUDE_PATH=/usr/local/include

How to force Eclipse to use g++ instead of gcc?

I already asked how to call a C++ constructor from a C file in How to call a C++ constructor from a C-File. Now when I successfully apply these methods suggested there, I receive an error
fatal error: string: No such file or directory compilation terminated
this error message points to the line: #include <string> in a header of a .cpp file.
I already found out that <string> is used by c++/g++ and <string.h> by c/gcc. Well the problem got clearer, when I checked the console output and there I can see, the (.cpp) file with the error was called by the gcc, which actually expects the <string.h> but that's not my intention - I need to compile it with the g++.
Now my question is: Can I force Eclipse to use a specific compiler? Here, for example just g++ (I heared it is capable of C-code too.) - Or even better, is there a way to chose the compiler for each directory in the workspace ?
Thanks for your advises
Answer respecting the wish of being able to specify the compiler for every subfolder:
What you are searching is probably a makefile project. That allows you to specify the toolchain, being for example the preprocessor, compiler and linker. g++ is an example for such a toolchain, as much as clang++ would be.
You can generate such a project in eclipse, writing the makefiles by hand, or use some build environment, such as CMake, which I would recommend for better portable code.
Both solutions would allow you to specify the compiler, as well as the compile flags, for every single directory of your project, if you wished so.
Writing a makefile for your existing C/C++ project can be achieved by completing the following steps:
in the folder where your source file is, right click and create a new file. New > File
name it makefile and click Finish
The new makefile should pop up in the editor and can be filled like follows:
makefile:
all: executable_name
clean:
-rm main.o executable_name[.exe on windows] executable_name
executable_name: main.o
g++ -g -o executable_name main.o
main.o: main.cpp
g++ -c -g main.cpp
Change Project's Setting can force eclipse to compile using g++: