I am writing a program that is cross platform. There are a few spots where I have to specify an operating system dependent call.
#ifdef WINDOWS
..do windows only stuff
#endif
#ifdef LINUX
..do linux only stuff
#endif
Are there any preprocesser directives that get defined by the compiler so I don't have to explicitly define them when I use the command line compiler. ie.
cl -DWINDOWS program.cpp
or
g++ -DLINUX program.cpp
I realize I could easily write a makefile or have a shell/batch script that will do this automatically. But I would prefer to use the same ones as the compiler (if they exist) by default.
I found a complete list for all g++'s on this website.
#define __HAVE_BUILTIN_SETJMP__ 1
#define __unix__ 1
#define unix 1
#define __i386__ 1
#define __SIZE_TYPE__ unsigned int
#define __ELF__ 1
#define __GNUC_PATCHLEVEL__ 3
#define __linux 1
#define __unix 1
#define __linux__ 1
#define __USER_LABEL_PREFIX__
#define linux 1
#define __STDC_HOSTED__ 1
#define __EXCEPTIONS 1
#define __GXX_WEAK__ 1
#define __WCHAR_TYPE__ long int
#define __gnu_linux__ 1
#define __WINT_TYPE__ unsigned int
#define __GNUC__ 3
#define __cplusplus 1
#define __DEPRECATED 1
#define __GNUG__ 3
#define __GXX_ABI_VERSION 102
#define i386 1
#define __GNUC_MINOR__ 2
#define __STDC__ 1
#define __PTRDIFF_TYPE__ int
#define __tune_i386__ 1
#define __REGISTER_PREFIX__
#define __NO_INLINE__ 1
#define _GNU_SOURCE 1
#define __i386 1
#define __VERSION__ "3.2.3 20030502 (Red Hat Linux 3.2.3-47.3)"
And Visual C++ already has Win32 defined to use for any Widnows version.
Yes, there are such pre-defined symbols but I don't recommend you use them unless you never, ever, forsee supporting more platforms, compilers or operating system versions. Here's the logic.
First, you're better off with your own minimal set of defined compilation constants for use within your code. Because you might start with things like this:
#if defined(_WIN32)
// do windows stuff
#endif
#if defined(_linux)
// linux stuff
#endif
Suppose you allow compilation under a new compiler for windows that doesn't define _WIN32 automatically? You can change every _WIN32 line to something like:
#if defined(_WIN32) || defined(ming)
Which is a real pain in the butt if you have more than one of those to change. You could put something like this at a high level:
#if defined(ming)
#define _WIN32
#endif
But you'll likely discover that some system or library header file freaks out because they happen to use _WIN32. Better you had abstracted it in a common header file:
#if defined(_WIN32) || defined(ming)
#define PLAT_WINDOWS
#endif
And then simply use #if defined(PLAT_WINDOWS) where needed.
But what about that common header file? Well, when a new compiler/OS/whatever comes along you have to start tweaking it to make sure it says the right things. Pretty soon it is a comical hairball of condition that can only be understood if you know about every version of every compiler on every operating system on the planet. And gee, who doesn't but nevertheless any changes made have to be tested everywhere and that is painful.
So, better you just have some high level setting inside the makefile or even outside it that says "if you're on windows, -DPLAT_WINDOWS" and be done with it.
Of course, all this is minimized if you use the most commonly available functions and features in your code.
All of these answers were very good. The solution that worked for me was the following.
#ifdef _WIN32
#endif
#ifdef linux
#endif
WIN32 was not defined by cl but _WIN32 was.
linux was defined by g++.
Yes. I believe WIN32 is, but I'm not on a windows right now to test it :D
See:
http://en.wikipedia.org/wiki/C_preprocessor (it mentions WIN32)
Instead of having two macros for specific platforms, you may have the code in #ifndef #else block. For example:
#ifndef _WIN32
// linux specific code
#else
// windows specific code
#endif
With this solution, the ifndef-else block will make sure that you don't accidentally add code in between the two #ifdef blocks (which supposedly be handling the program execution flow in same manner).
The above code will also give you independence of compiling this code not just for linux but any unix platform, provided it supports the call, without changing the macro label from linux to unix or something else.
Related
Ultimately I want to end up with a single set of source files that compiles to a Windows or Linux dynamic library depending on which platform compiled it.
The problem seems to be that Windows requires that annotations be made to both the header file declarations and the source file definitions. DLL Tutorial For Beginners
Linux dynamic link libraries seem to require annotations only in the source file definitions.
I can #define a preprocessor string to handle the difference of the source code definitions.
#if (_MSC_VER >= 1900) // (Visual Studio 2015 version 14.0)
#define EXPORTED __declspec(dllexport)
#else
#define EXPORTED __attribute__((visibility("default")))
#endif
Both Windows and Linux ignore empty #define statements.
You may not use annotations in Windows. You can use DEF file for declare export functions .def files C/C++ DLLs
For linux you need to use annotations, e.g.:
int
#ifdef __GNUC__
__attribute__((visibility("default")))
#endif
myfunction(int param) {
return 0;
}
This solves the problem that Windows requires functions
in the DLL header and the DLL source file be annotated
and Linux requires only the LIB source functions to be
annotated. Both Windows and Linux ignore empty #define
statements.
//
// Dynamic Link Library for Linux and Windows
//
// If not >= Visual Studio 2015 (version 14.0) then
// Linux is assumed
//
// This file is inlcluded in all of the LIB/DLL Source
// and the LIB/DLL caller.
//
#if (_MSC_VER >= 1900) // (Visual Studio 2015 version 14.0)
// Windows Function Definition (LIB/DLL Source)
#define EXPORTED_DEF __declspec(dllexport)
// DLL_EXPORT is defined at top of LIB/DLL Source of exported functions
#if defined DLL_EXPORT
// Windows Function Declaration (DLL Header)
#define EXPORTED_DEC __declspec(dllexport) // DLL Export
#else
// Windows Function Declaration (Caller Header)
#define EXPORTED_DEC __declspec(dllimport) // DLL Import
#endif
#else
// Linux Function definition (LIB/DLL Source)
#define EXPORTED_DEF __attribute__((visibility("default")))
#define EXPORTED_DEC // Linux cannot see this
#endif
I've noticed that several of our projects do the whole
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
thing in their stdafx.h.
Will this memory leak detection work as intended when specified in the stdafx?
Don't put it in stdafx.h. Doing so can give you undesired side effects.
Here's why.
In most cpp files, you have something like this:
#include "stdafx.h"
#include <AcmeHeader.h>
#include "MyHeader.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
Note that the redefinition of new is explicitly supposed to happen after all headers are included. If you define DEBUG_NEW in stdafx.h, then that definition will also be applied to AcmeHeader.h and MyHeader.h, which can cause problems with headers that try to redefine operator new.
I've also run into cases where I've wanted to remove the redefinition of "new" for just one or two files, but that's a rare situation.
I'm developing a chat server.
this is my source.
#if defined(_WIN32) || defined(_WIN64)
#ifdef PENGCHAT3SERVERLIB_EXPORTS
#define PENGCHAT3SERVER_API __declspec(dllexport)
#else
#define PENGCHAT3SERVER_API __declspec(dllimport)
#endif
#elif defined(__unix) // is this right?
#define PENGCHAT3SERVER_API //....?????
#endif
I have two questions.
first, defined(__unix) is ok?
second, what is keyword dynamic library export / import in linux?
I'm very wondering.
Please help me
thanks.
If you're building on Linux, there is no need for a specifier or special measures; all function symbols are exported by default.
#define PENGCHAT3SERVER_API
You want to look for the __unix__ (preferred) or unix defines, but you should consider making it the default instead.
If I'm going to disable warnings specific to compiler say VC++ and run this:
#pragma warning( disable : xxxx )
on comeau or g++, how do i make sure the preprocessor won't be executed it?
To detect the MSVC compiler you can see if _MSC_VER is defined so you could do something like
#ifdef _MSC_VER
#pragma warning( disable : xxxx )
#endif
Predefined Macros gives information about predefined VS macros
See this resource for a list of macros to identify which compiler is being used. Use them as follows (for MS VC):
#ifdef _MSC_VER
#pragma warning( disable : xxxx )
#endif
I need a set of wrappers around the standard system calls-open,listen,etc.
For these i have a few "#define" as in:
#define open(a,b,c) JCL_Open(a,b,c)
But when i compile the header and associated .c files, i am getting the following warning:
/jcl_wrappers.h:114:1: warning: "open" redefined
/jcl_wrappers.h:113:1: warning: this is the location of the previous definition
How can i suppress these warnings?
Put include guards in your header file.
Basically you need to put these two lines at the beginning of your header file
#ifndef _yourincludefilename_h_
#define _yourincludefilename_h_
and
#endif /* _yourincludefilename_h_ */
at the end of you include file.
Edit:
rascher is right, that open is not a good name for a macro, as it will conflict with a library function. Usually it is good C convention to make macros all uppercase, so I would suggest to change your macro to
#define OPEN(a,b,c) JCL_Open(a,b,c)
or even better
#define XYZ_OPEN(a,b,c) JCL_Open(a,b,c)
where XYZ is a prefix specific to your code.
Leave the standard functions alone and rename the function:
#define myopen(a,b,c) JCL_Open(a,b,c)
Someone will thank you later.
You might try using compile guards. Like:
my_headers.h:
#ifndef __MY_HEADERS
#define __MY_HEADERS
#define open(a,b,c) JCL_Open(a,b,c)
#endif
This will only do do what is between the #ifndef and #endif if the '__MY_HEADERS' macro has been defined. So, everything in your .h file will only be declared once.
You could use the same construct to see if the "open" macro is already defined.
Also be aware that there is already a C function called open(): http://www.manpagez.com/man/2/open/ . May not be a great idea to use the same name for your macro!
Wrap the define in:
#ifndef JCL_WRAPPERS_H
#define JCL_WRAPPERS_H
... your macro's
#endif
This makes sure your macro's are only defined once if you including your header in multiple places.
It's generally a smart thing to do for all prototypes in headers also.