Using SDL with the g++ compiler - c++

I'm trying to run this test code from the SDL website but I dont understand where to put the downloaded files and how to reference them using the g++ compiler. I've been trying to using the -I command but I dont quite understand that either. I keep getting the "fatal error: SDL.h: No such file or directory #include<SDL.h> "
Using windows and sublime text editor and g++ compiler

Directly from the g++ manual:
"-I dir
Add the directory dir to the list of directories to be searched for header files. Directories named by -I are searched before the standard system include directories. If the directory dir is a standard system include directory, the option is ignored to ensure that the default search order for system directories and the special treatment of system headers are not defeated . If dir begins with "=", then the "=" will be replaced by the sysroot prefix; see --sysroot and -isysroot."
As for your error, I would try #include <SDL2/SDL.h>. This could vary based on the version you are using. If that doesn't work I would just make sure you have the correct path when including in the compiler as a flag.

Related

How to #include <whatever.h> installed with apt in Ubuntu

I have just installed hidapi in my Ubuntu 20.04 following the instructions, i.e. by doing
sudo apt install libhidapi-dev
I wrote my program in the file mwe.cpp which contains only this line:
#include <hidapi.h>
and now I want to compile it with
g++ -o mwe.o mwe.cpp
but I get
mwe.cpp:1:10: fatal error: hidapi.h: No such file or directory
How am I supposed to use this module? Sorry for such a basic question but cannot find out.
On Ubuntu based systems, the system package libhidapi-dev installs the include files to /usr/include/hidapi, so either include this (-I/usr/include/hidapi) in your command line or #include <hidapi/hidapi.h>
If that header is not within the standard search path for headers, then you can include it manually with the -I flag e.g.
g++ -I/usr/include/hidapi -o mwe.o mwe.cpp
To locate a file on ubuntu, you can run:
sudo updatedb
locate hidapi.h
> /usr/include/hidapi/hidapi.h
You can view the standard include search path with:
gcc -print-search-dirs
Alternatively, because /usr/include is on the standard search path, you can write your include as <hidapi/hidapi.h>.
How to #include <whatever.h> installed with apt in Ubuntu
If the package installs the header within a directory included in the default search path of your compiler - which is typical - then you can include the header using a relative path from the root of the search path where the header is installed. For example, if the file is in the path /usr/include/x/y.h, then you can include <x/y.h>.
If the package doesn't install the header within the default search path, then you must specify the search path for the compiler when you invoke it, and then include the header relative to the specified include directory. For example, if the file is in the path /opt/custom/x/y.h, then you can include <x/y.h> and specify /opt/custom as a search path for the compiler.
If you use GCC or compatible compiler, and the package supports it, then you can use a program called pkg-config to get the compiler options needed to use the library. Besides the header search path, this also takes care of linking with the library as well as any mandatory compiler options. Example:
pkg-config --libs --cflags libhidapi
I don't know, how do I find the location of hidapi.h?
There are several ways to find out the location of a file. A general tool is the program find. Example:
find / -name=hidapi.h
A more specific tool for learning the paths of files installed by an apt package is apt-file. Alternatively, you can look up the list of files in the https://packages.ubuntu.com/ website.

MinGW g++/gcc not compiling with proper path and version command working

i changed the path to the correct directory and when I typed "g++ --version" and "gcc --version" I get the version info. I saved simple.cpp into a folder I made in the directory from path and typed "g++ simple.cpp" into the console and it returned with
C:\Users\guede>g++ simple.cpp
g++: error: simple.cpp: No such file or directory
g++: fatal error: no input files
compilation terminated.
This is the code i am trying to compile as a g++ test
#include <iostream>
int main()
{
std::cout << "Hello World!\n";
return 0;
}
g++ simple.cpp
will look for simple.cpp in the current directory. Since You are executing g++ from C:\MinGW\bin, the compiler will only look in C:\MinGW\bin. It will not look in subdirectories. Since simple.cpp is in C:\MinGW\bin\Cpp_prog, the compiler is not looking for the file in the right place. You could
g++ Cpp_prog\simple.cpp
to specify which directory the file is in, but you do not want the compiler output cluttering up GCC's bin directory. Instead, make yourself a "Workspace" directory somewhere else on the computer, could be in your Documents folder or anywhere else convenient. Inside this workspace make a separate directory for each project so you can keep things organized more easily. Execute the compiler from within the appropriate project directory.
Then either invoke the compiler with
C:\MinGW\bin\g++ simple.cpp
or add C:\MinGW\bin to the user path or system path. See Adding directory to PATH Environment Variable in Windows for help on that.
Later you may find yourself with many different compilers and sometimes several versions of GCC. You don't want to have many locations for g++ in the path because it's easy to call the wrong one. When you get to this point you will likely have had to learn about using build automation tools to assist you with complicated projects. Typically you'll specify the compiler location to the automation tool.
Side note:
dir is the DOS /Windows directory listing command. In the command prompt, type dir it will list all of the files in the current directory. If you're going to take up programming, you'll find it very helpful to learn about the various commandline tools provided by your operating system and development environment.

How to Place non-Default C++ Packages in Cygwin File Structure Correctly

I'm a bit of a novice at C++ development. The goal is to download an correctly ready a new package so that Cygwin will understand the statement #include "pcap.h". The directions for the developer package for WinpCap were pretty straight forward:
Download the ZIP archive containing the developer's pack
Uncompress it to the desired folder
Developer Package Instructions Link
The part that I am having trouble with is the "desired folder" part. I found the path for where Cygwin is storing default libraries which for me is C:Cygwin64/usr/include . I placed the package in this directory but that didn't work. pcap.h is nested in the unzipped folders two or three directories in. I noticed that this is a little different when compared to the directories that were there by default. To test whether or not this was correct or not I simply did an #include "pcap.h" statement in a .cpp file that previously compiled with no issues. With the new included statement I got a
fatal error: pcap.h: No such file or directory
from Cygwin.
How should I go about solving this? The goal is to have this behave like any other directory one would want.
You could use this to find where gcc searches for the header files.
`gcc -print-prog-name=cc1plus` -v
The result of the command in a Cygwin console :
$ `gcc -print-prog-name=cc1plus` -v
ignoring nonexistent directory "/usr/local/include"
ignoring nonexistent directory "/usr/lib/gcc/i686-pc-cygwin/4.9.2/../../../../i686-pc-cygwin/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/lib/gcc/i686-pc-cygwin/4.9.2/include/c++
/usr/lib/gcc/i686-pc-cygwin/4.9.2/include/c++/i686-pc-cygwin
/usr/lib/gcc/i686-pc-cygwin/4.9.2/include/c++/backward
/usr/lib/gcc/i686-pc-cygwin/4.9.2/include
/usr/lib/gcc/i686-pc-cygwin/4.9.2/include-fixed
/usr/include
End of search list.
You could still maybe place your package's headers in one of them.

How to set Eclipse Library paths?

I am trying to get MLPack to work in Eclipse, but have some problems with including a header file.
I manage to read a header file in Eclipse
#include <neighbor_search.hpp>
This header file calls itself
#include <mlpack/core.hpp>.
I included in Eclipse in the library path the path just upto mlpack/core.hpp, i.e.
/usr/include/MLPack/mlpack-1.0.8/src in the includes tab of paths and symbols.
I get the error message though:
/usr/include/MLPack/mlpack-1.0.8/src/mlpack/methods/neighbor_search/neighbor_search.hpp:26:27: fatal error: mlpack/core.hpp: No such file or directory
#include <mlpack/core.hpp>
How do set my path correctly so that <mlpack/core.hpp> will be found?
I also use MLPack (but not in Eclipse) and had this error.
To resolve this problem you have to specify to GCC where the files of mlpack are. The thing is when you include a header file with #include <file.h> , GCC looks in these directory :
/usr/local/include
libdir/gcc/target/version/include
/usr/target/include
/usr/include
So what I did is create a soft link in /usr/include/ to the mlpack directory :
cd /usr/include/
sudo ln -s /full/path/to/the/mlpack/folder/ mlpack
Like that the GCC will have access to mlpack directory.
You'll have to download and install at least these 2 libraries (if you don't have them) :
boost and armadillo.
Personally I also had to create a soft link for the libxml library :
cd /usr/include/
sudo ln -s /usr/libxml2/libxml/ libxml
I'm a bit in late to answer, but I hope it'll help further people !
http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.cdt.doc.user%2Freference%2Fcdt_u_prop_general_pns_libpath.htm
Project Properties -> C/C++ General category -> Paths and symbols -> Includes tab

g++ compile using custom header file

I want to use a header file,
its included as #include <custom.h>
how can I compile it by using the custom.h header file ?
I tried -I /path/to/custom.h , but its giving me error that its not a directory..
-I /path/to
This will enable all headers in that directory to be found.
In man gcc, search (using / in your pager) for "-I dir":
-I dir
Add the directory dir to the list of directories to be searched for header files. Directories named by -I are searched before the standard system include directories. If the directory dir is a standard system include directory, the option is ignored to ensure that the default search order for system directories and the special treatment of system headers are not defeated. If dir begins with "=", then the "=" will be replaced by the sysroot prefix; see --sysroot and -isysroot.
Indeed /path/to/custom.h is not a directory but a file.
-I/path/to/custom/