Handling #include paths on different platforms - c++

I'm developing a software on an Arch Linux platform with NCurses. It has an #include <ncurses.h> in a global header.
My collaborator works in Windows and emulates a bash shell with Cygwin. However, cygwin installs ncurses libraries such that you have to use #include <ncurses/ncurses.h>. It's trivial to change, but our Git history could consist of that line changing back and forth multiple times if one of us forgot which seems silly.
Is there some kind of preprocessor check I could do to use the appropriate path?

As #BoBTFish said, you should specify the include path in the Makefile (different for each platform). If not, you can use the macro __CYGWIN__ (or __CYGWIN32__, not sure which one will work) to detect if you're using cygwin, like
#ifdef __CYGWIN__
#include <ncurses/ncurses.h>
#else
#include <ncurses.h>
#endif
See http://homepage.ntlworld.com/jonathan.deboynepollard/FGA/predefined-macros-platform.html for more details.

Related

#include <Eigen/Dense> failed, #include "Eigen/Dense" succeeds - why?

While setting up the Eigen library, I tried:
#include <eigen/Eigen/Dense>
Which results in the error:
eigen/Eigen/Dense: No such file or directory
However,
#include "eigen/Eigen/Dense"
succeeds.
Why is this? Based on previous questions asked, I suspect this is because #include <> searches in the system directory and #include "" searches locally. If this is true, how would one put the Eigen folder in the system directory?
You will need to follow the instructions that are specific for your operating system. You did not describe your operating system and compiler, and this is something for which no standard instructions apply for every operating system used in the world today. It varies by operating system and compiler.
Alternatively, all C++ compilers have a configuration setting that adds an additional directory to the list the compiler looks when searching for #include <path> header files. gcc, and many other Linux compilers use the -I option. This can be specified manually, or in a Makefile.
MS-Windows compilers have a specific settings too. This Stackoverflow question provides instructions for several versions of the Visual Studio IDE.

How to check if Xlib is used using #ifdef?

For example you can check if Windows is used by checking if "WIN32" macro is defined. And i would like to get the same behaviour but to check if Xlib is used. But i don't know what Xlib defines to know it's defined.
If you don't know what i mean here is an example:
#ifdef WIN32 //Check if WIN32 is defined
//Do something
#endif
And i would like to change this in a way that it does something when Xlib used.
I'm sorry if there are some grammatical errors but i'm not a native english speaker.
The macro WIN32 is only defined, if you included #include <windows.h> before or set the macro in your compiler flags (-DWIN32). For Xlib you could use the macro X_PROTOCOL very similar if you included #include <X11/X.h> before.
#ifdef X_PROTOCOL //Check if X_PROTOCOL is defined
//Do something
#endif
Another way would be to use _XLIB_H_ if you included #include <X11/Xlib.h> before, but I wouldn't because identifiers beginning with an underscore are reserved to the implementation and there is no guarantee that this identifiers won't change.
you can check if Windows is used by checking if "WIN32" macro is defined
The WIN32 macro is defined by default by compilers that target Windows OS. Its purpose is to let the program detect Windows OS API, not necessarily any graphic subsystem.
X11 is not and OS. It's optional userland software that installs and runs under many operating systems, including Windows. When compiling an X11 programs for Windows, WIN32 will be defined, just like for any other Windows program.
No compiler defines anything by default on a system where X11 is installed.
If you want to detect X11 at build time, your best bet is probably a meta-build tool like GNU autotools or CMake.

How to use C Libraries for Arduino code

I have a code in Visual studio that I want to implement in Arduino. But there is a problem. Many libraries usable in Visual Studio aren't usable in Arduino IDE. How can I use them in my Arduino code. To be precise, the libraries I want to use are
#include <iostream>
#include <iomanip>
#include <queue>
#include <string>
#include <math.h>
#include <ctime>
respectively.
Okay so I know I have <iostream> available in Arduino. <math.h> is also available I think along with <string> library.
The main problem is to how to use #include <queue> and its functions such as priority_queue() and other fucntions of iostream like .pop()?
Arduino behind the scenes is using the avr-gcc compiler, which provides support for many of the features of the C++ language. It does not, however, include an implementation of libstdc++, which means that a lot of the libraries and features you are used to having with other development environments are just not there. A big reason for this is that it is just not practical to implement some of that functionality on a small microcontroller.
There are several libraries available that implement simplified versions of some of the functions and data structures you are wanting to use. You can find a list (but not necessarily a complete one) of these libraries here:
http://playground.arduino.cc/Main/LibraryList
For example QueueList might be a good alternative to <queue>.
Whatever you find, you are likely to have to refactor your code to use them. When you run into problems implementing those libraries and changes, I would recommend heading over to https://arduino.stackexchange.com/ to get more arduino specific answers.
It is detailed over here:
https://www.arduino.cc/en/Hacking/BuildProcess
The include path includes the sketch's directory, the target directory
(/hardware/core//) and the avr include directory
(/hardware/tools/avr/avr/include/), as well as any library
directories (in /hardware/libraries/) which contain a header
file which is included by the main sketch file.
And these are the libraries supported by avr-gcc (the compiler that Arduino uses)
http://www.nongnu.org/avr-libc/user-manual/modules.html

'glGenBuffers' was not declared in this scope. Windows 7. Qt 4.8.1

'glGenBuffers' was not declared in this scope
thats the error which I get after trying to use that metod. How to add appropriate libs or sth else and what i should add and how. I'm using win 7 and qt 4.8.1. I've read about GLEW but I don't know how to add it to windows or qt.
It's a FAQ. Windows defines the OpenGL ABI (B for binary interface) only up to version OpenGL-1.1 – anything beyond that must be loaded at runtime using the extension loading mechanism. Most easily done by using a wrapper library like GLEW.
I've read about GLEW but I don't know how to add it to windows or qt.
If you want to use any 3rd party library, you've to learn how to do that. It boils down to install the library and its header in some system wide directory, but outside the regular compiler tree, and add those directories to the include and library search paths.
I use this code for including GLEW and Glut for cross-platform development:
#ifndef GL_H
#define GL_H
#ifdef __APPLE__
#include <GL/glew.h>
#include <GLUT/glut.h>
#else
#include <GL/glew.h>
#include <GL/glut.h>
#endif
#endif // GL_H
I encapsulate that into a file called gl.h and include that anywhere I need an OpenGL context. You will need to research how to install GLEW on your system. Make sure that you install the correct build of GLEW for your compiler. If you use Visual C++ 2010 or MinGW, it will be slightly different as far as where to put the libs and include files. You could always build from source too, but that is a more advanced route.
If you would like some sample code on how to get started with Qt and OpenGL, I've got a repository on Bitbucket with some code that I wrote for an OpenGL programming class here: https://bitbucket.org/pcmantinker/csc-4356/src/2843c59fa06d0f99d1ba90bf8e328cbb10b1cfb2?at=master

Making an OpenGL app without Windows.h

I've set the linker to additional dependencies opengl32.lib;glu32.lib;sfml-main-d.lib;
When I run it, I get loads of errors about APIEntry or something in one of the OpenGL files. SFML is also set up fine.
Here is my code:
#include <iostream>
//#include <Windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
using namespace std;
void main()
{
}
It works fine if I include Windows.h, but I really don't want to make it windows-specific (Since the only reason I switched to C++ from C# is for cross platform and I'm not too fussed on Java)
If you're going to use OpenGL, then you should employ a proper OpenGL Loading Library to get your function pointers. These libraries have headers that will include whatever platform-specific stuff is needed to make the header work, using appropriate #defines and so forth.
If you're starting out you're probably not interested in the lower level stuff as setting up your own OpenGL context and such. I would recommend you take a look at GLFW at http://www.glfw.org/ - it is what I prefer for OpenGL. It is open source and cross platform for both Windows, linux and MAC.
Good luck!