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.
Related
For my project I'm required to compile my program on both Microsoft Visual Studios (windows) and g++ (unix).
My program works when on windows, but when on g++ I get the following error:
utilities.cpp:4:21: fatal error: windows.h: No such file or directory
#include <windows.h>
I feel like I messed up when separating the header file and utilities.cpp.
For reference here is the globals.h and the utilities.cpp files:
http://imgur.com/a/GhhCl
You can't use <windows.h> on a linux machine
Use ifdef to choose what preprocessor commands run. More specifically, you can check if the operating system is windows with the pre-defined __WINDOWS__ macro. Most compilers
#ifdef __WINDOWS__
#include <windows.h>
#endif
You can also try _WIN32
The reason for your compilation failure has been explained by #NickLamp , but only that macro won't make your program work properly in Xnix.
It seems that you are building CLI interface, and instead of using OS-specific APIs, I recommend you use some cross-platform CLI libraries:
Cygwin + ncurses or pdcurse.
See http://www.cplusplus.com/articles/4z18T05o for details.
Unlike the Java, C and C++ are not platform independent languages. Some of their things like - libraries files, pre-defined objects and functions may vary from one Operating System to another.
If you want to compile your file on Ubuntu, then you will have to either remove the <windows.h> header file from your file or use some techniques (like - macros) which ensure against the use of <windows.h> on the Operating Systems other than Windows. Because this header-file is strictly designed for Windows.
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:
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.