I am facing issues with Xcode 5.1. Everything works fine if I do this:
#include "glew.h"
But as soon as I try to make it cross-platform, it starts giving the compilation error.
#if defined(_APPLE_) || defined(_MACH_)
#define OS_MACOSX
#endif
#ifdef OS_MACOSX
#include "glew.h"
#else
#include <GL/glew.h>
#endif
Please help me to understand this.
You may want to use __APPLE__ instead of _APPLE_. That should solve it.
Oh, and don't moan at the IDE, it's got nothing to do with the IDE. It's about where Apple's developers decided to place the GL header files (and CL header files for that matter!)
Sounds like you simply need to add either a "GL" framework, or provide a proper "header search path" in your Xcode project.
Do you know the full path to the "<GL/glew.h>" file? If so, you can add the full path to your project settings.
Which you can do like this:
Related
I am porting my c/cpp project from gcc/Linux to Windows using MSYS2/MinGW64 environment. Two of the headers were not found where expected and I had to modify include in the way bellow in order to achieve succesful compilation.
#ifdef __MINGW64__
#include <ncurses/ncurses.h>
#else
#include <ncurses.h>
#endif
and
#ifdef __MINGW64__
#include <json/json.h>
#else
#include <jsoncpp/json/json.h>
#endif
However I feel this solution is not correct (or is it?). I think I probably missed something obvious, may be some configuration step. What can I do better?
BTW: I use simple make file which calls g++ with the same set of parameters in Linux and Windows and I do not specify any paths there.
Thank you in advance!
For jsoncpp, you can run this command on both Linux and MSYS2 to get the recommended compiler flags:
pkg-config --cflags jsoncpp
If you pass those flags to the compiler, then I think a simple #include <ncurses.h> should work on both platforms.
That does not work for ncurses, unfortunately, because there is no configuration file for ncurses /mingw64/lib/pkgconfig/ in MSYS2.
When i compile my C++ solution in vs2010 x64 mode, i get the below compilation issue.
Can not open include file 'sys/param.h' :No such file or directory.
But the same compiles fine in Win32 mode.
I am not sure how this header file is missing.Can any one help me on this?
I am using some of the client headers and this is the below code section that is present in the client file.
#ifndef WIN32
#include <sysipc.h>
#include <sys/param.h>
#endif
The include #include <sysipc.h> should be #include <sys/ipc.h>, however, this is a POSIX header file that is meant for Linux build projects so it won't work for any Visual Studio projects. Since you're compiling for x64, WIN32 flags might not be set by default.
Try changing the macro to:
#ifndef _MSC_VER
#include <sys/ipc.h>
#include <sys/param.h>
#endif // !_MSC_VER
Hope that helps.
This is highly likely a consequence of some #if going wrong - e.g. it's checking for _M_IX86, and it not being set on a 64-bit system, it picks up something non-windows and tries to compile that.
sys/param.h is a unix/linux header-file, and you shouldn't expect to find that in your Windows system. [edit: unless you hooked in a version of the GNU compiler or did some other modification to the compilation tools core of your MSVC build environment]
Unfortunately, without seeing the source code, all we can possibly do is explain the possible reasons...
I am working on a C++ project using Xcode on MacOS X, and am now starting to port it to Linux using the Code::Blocks IDE.
Many of my source files are in separate directories and I am having issues including them.
Here is an example of this issue:
folder1/foo.h
folder2/dog.h
foo.h includes dog.h with: `#include "dog.h"`
It works fine on Xcode if both files in the same project but if I try it in Code::Blocks it has an error finding it.
I can fix this issue in Code::Blocks by changing the code to use a relative include path such as:
#include "../folder2/dog.h"
Unfortunately doing this stops Xcode from being able to find the file.
How can I fix this issue so I can compile the same code in multiple IDEs? I would like to avoid throwing all the source in the same folder. Should I use a preprocessor statement similar to:
#if XCODE
#include "dog.h"
#else
#include "../folder2/dog.h"
#endif
Rearrange your structure so that one project has only one common include directory:
/project/
/src/*.cpp
/include/*.hpp
/folder1/dog.hpp
/folder2/cat.hpp
Now say #include <config.hpp> and #include <folder1/dog.hpp> etc., and add to your compiler flags:
-I ${PROJECT_DIR}/include
How a given compiler/IDE locates dependencies is, unfortunately, entirely compiler/IDE-specific. There is no way to arrange this in such a way that it will be honoured by all development environments.
I don't know Xcode or Codeblocks, but I'm sure there must be some project configuration that controls where they looks for #include files.
I am using Visual Studio 2010. I have read that in C++ it is better to use <cmath> rather than <math.h>.
But in the program I am trying to write (Win32 console application, empty project) if I write:
#define _USE_MATH_DEFINES
#include <math.h>
it compiles, while if I write
#define _USE_MATH_DEFINES
#include <cmath>
it fails with
error C2065: 'M_PI' : undeclared identifier
Is it normal? Does it matter if I use cmath or math.h? If yes, how can I make it work with cmath?
UPDATE: if I define _USE_MATH_DEFINES in the GUI, it works. Any clue why this is happening?
Interestingly I checked this on an app of mine and I got the same error.
I spent a while checking through headers to see if there was anything undef'ing the _USE_MATH_DEFINES and found nothing.
So I moved the
#define _USE_MATH_DEFINES
#include <cmath>
to be the first thing in my file (I don't use PCHs so if you are you will have to have it after the #include "stdafx.h") and suddenly it compile perfectly.
Try moving it higher up the page. Totally unsure as to why this would cause issues though.
Edit: Figured it out. The #include <math.h> occurs within cmath's header guards. This means that something higher up the list of #includes is including cmath without the #define specified. math.h is specifically designed so that you can include it again with that define now changed to add M_PI etc. This is NOT the case with cmath. So you need to make sure you #define _USE_MATH_DEFINES before you include anything else. Hope that clears it up for you :)
Failing that just include math.h you are using non-standard C/C++ as already pointed out :)
Edit 2: Or as David points out in the comments just make yourself a constant that defines the value and you have something more portable anyway :)
Consider adding the switch /D_USE_MATH_DEFINES to your compilation command line, or to define the macro in the project settings. This will drag the symbol to all reachable dark corners of include and source files leaving your source clean for multiple platforms. If you set it globally for the whole project, you will not forget it later in a new file(s).
This works for me:
#define _USE_MATH_DEFINES
#include <cmath>
#include <iostream>
using namespace std;
int main()
{
cout << M_PI << endl;
return 0;
}
Compiles and prints pi like is should: cl /O2 main.cpp /link /out:test.exe.
There must be a mismatch in the code you have posted and the one you're trying to compile.
Be sure there are no precompiled headers being pulled in before your #define.
This is still an issue in VS Community 2015 and 2017 when building either console or windows apps.
If the project is created with precompiled headers, the precompiled headers are apparently loaded before any of the #includes, so even if the #define _USE_MATH_DEFINES is the first line, it won't compile. #including math.h instead of cmath does not make a difference.
The only solutions I can find are either to start from an empty project (for simple console or embedded system apps) or to add /Y- to the command line arguments, which turns off the loading of precompiled headers.
For information on disabling precompiled headers, see for example
https://msdn.microsoft.com/en-us/library/1hy7a92h.aspx
It would be nice if MS would change/fix this. I teach introductory programming courses at a large university, and explaining this to newbies never sinks in until they've made the mistake and struggled with it for an afternoon or so.
With CMake it would just be
add_compile_definitions(_USE_MATH_DEFINES)
in CMakeLists.txt.
According to Microsoft documentation about Math Constants:
The file ATLComTime.h includes math.h when your project is built in Release mode. If you use one or more of the math constants in a project that also includes ATLComTime.h, you must define _USE_MATH_DEFINES before you include ATLComTime.h.
File ATLComTime.h may be included indirectly in your project. In my case one possible order of including was the following:
project's "stdafx.h" → <afxdtctl.h> → <afxdisp.h> → <ATLComTime.h> → <math.h>
As suggested by user7860670, right-click on the project, select properties, navigate to C/C++ -> Preprocessor and add _USE_MATH_DEFINES to the Preprocessor Definitions.
That's what worked for me.
I am including a third party header and source file into my project.
At the top of the header there is this:
#if defined(WIN32) || defined(WIN16)
#ifndef MSDOS
#define MSDOS
#endif
#endif
#include <stdio.h>
#include <stdlib.h>
#ifndef MSDOS
#include <unistd.h>
#endif
#include "des.h"
The problem is that #if defined(WIN32) fails and the compilation fails when trying to #include unistd.h which I don't want to do.
I have third party project that works with this header file i.e. WIN32 is defined and it doesn't try to include In Visual Studio I did "Go To Definition" on "WIN32" and was taken to the following definition in WinDefs.h.
#define WIN32
I'm not sure this is where its getting WIN32 definition from, as the third party project does not appear to include "WinDefs.h".
So my problem is, how can I get WIN32 to be defined in my current new project?
Depends on your project setup. WIN32 is defined inside the windows header files, but you can pass it to the compiler as well ("-DWIN32" for gcc for example). Try it and see whether it compiles.
Visual Studio has the built-in define _WIN32. mingw-gcc has WIN32 and _WIN32 built-in so the project was likely tested using gcc. You might add
#if defined(_WIN32) && !defined(WIN32)
#define WIN32
#endif
or just add a -DWIN32 to the CFLAGS.
Check your includes. I am guessing that the third party header is included prior to the windows.h. So, in your main.cpp or equal it should be
#include <windows.h> // this will also include windefs.h
#include <thirdParty.h>
and not the other way around.
Hope that helps.
You can simply include the windows header files (windows.h) before including the third party header - as you already found out WIN32 is defined there but technicaly it could be defined anywhere (so if the third party project is not including the windows headers check if it's being defined in the compiler project settins directly).
BTW there is also a _WIN32 define that is set by the compiler, it's possibly a better idea to look for this define if checking if the code is being compiled under windows;
For those seeking answers to the
where is WIN32 defined
part of the questions, I've found it defined in:
minwindef.h
ole2.h
Note, I have no confidence that these are the only places it's defined. I expect there are probably other files where it's defined. Nevertheless, I thought this might help some people.
Some WIN32 defined in the compiler . Just like this,If you use the gcc for windows , WIN32 is defined . If you use the gcc for linux , WIN32 is not defined :)
So , the macros is a switch. You can define it to use somethine , and not define it to unuse something.