Compilation on linux with defined target - c++

I want to compile a project with a Makefile.
I have defined target for linux in one of my .h
#define LINUX_TARGET (COMPILER_GCC_4_4_1|FAMILY_LINUX|TYPE_X86)
...
#ifdef _LINUX_TARGET_
#define __linux__
#define TARGET LINUX_TARGET
...
#ifdef __linux__
#define __LINUX__
#endif
So in my Makefile I say that, I will use this target:
...
CFLAGS += -D_LINUX_TARGET_
...
Butwhen I compile i get this error :
../../../../../../../Generic/Common/Include/target_definition.h:145:0: warning: "__linux__" redefined [enabled by default]
#define __linux__
^
../../../RefFT/Float/src/LIMITOR_main_32f.c:1:0: note: this is the location of the previous definition
/*---------------------------------------------------------------------------
^
I don't understand why, beceause this works for macos or windows target...
EDIT
After Joachim Pileborg answer, what I did :
#define LINUX_TARGET (COMPILER_GCC_4_4_1|FAMILY_LINUX|TYPE_X86)
...
#ifdef _LINUX_TARGET_
#ifndef __linux__
#define __linux__
#endif
#define TARGET LINUX_TARGET
...
#ifdef __linux__
#define __LINUX__
#endif

Because __linux__ is pre-defined when building on Linux.

Related

Strange warning when using #define XX __pragma

I'm using the following preprocessor definitions to disable specific warnings for different c++ compilers.
Here's the code
#if defined(_MSC_VER)
#define DISABLE_WARNING_PUSH __pragma(warning(push))
#define DISABLE_WARNING_POP __pragma(warning(pop))
#define DISABLE_WARNING(warningNumber) __pragma(warning(disable : (warningNumber)))
#define DISABLE_WARNING_DEPRECATED_FUNCTION DISABLE_WARNING(4996)
#elif defined(__GNUC__) || defined(__clang__)
#define DO_PRAGMA(X) _Pragma(#X)
#define DISABLE_WARNING_PUSH DO_PRAGMA(GCC diagnostic push)
#define DISABLE_WARNING_POP DO_PRAGMA(GCC diagnostic pop)
#define DISABLE_WARNING(warningName) DO_PRAGMA(GCC diagnostic ignored #warningName)
#define DISABLE_WARNING_DEPRECATED_FUNCTION DISABLE_WARNING(-Wdeprecated-declarations)
#else
#define DISABLE_WARNING_PUSH
#define DISABLE_WARNING_POP
#define DISABLE_WARNING_DEPRECATED_FUNCTION
#endif
These defines works for GCC and CLANG but not for MSVC compiler.
On MSVC compiler I get the following warning:
(23): warning C4081: expected ')'; found '('
You can see it here: https://godbolt.org/z/hTErEbG7W
How can I fix this warning for MSVC compiler?

How to get rid of warnings that precompiler definitions are not definied

There is a file that I downloaded from the Unity web-site
#pragma once
// Standard base includes, defines that indicate our current platform, etc.
#include <stddef.h>
// Which platform we are on?
// UNITY_WIN - Windows (regular win32)
// UNITY_OSX - Mac OS X
// UNITY_LINUX - Linux
// UNITY_IOS - iOS
// UNITY_TVOS - tvOS
// UNITY_ANDROID - Android
// UNITY_METRO - WSA or UWP
// UNITY_WEBGL - WebGL
#if _MSC_VER
#define UNITY_WIN 1
#elif defined(__APPLE__)
#if TARGET_OS_TV //'TARGET_OS_TV' is not defined, evaluates to 0
#define UNITY_TVOS 1
#elif TARGET_OS_IOS //'TARGET_OS_IOS' is not defined, evaluates to 0
#define UNITY_IOS 1
#else
#define UNITY_OSX 1 //'UNITY_OSX' macro redefined
#endif
#elif defined(__ANDROID__)
#define UNITY_ANDROID 1
#elif defined(UNITY_METRO) || defined(UNITY_LINUX) || defined(UNITY_WEBGL)
// these are defined externally
#elif defined(__EMSCRIPTEN__)
// this is already defined in Unity 5.6
#define UNITY_WEBGL 1
#else
#error "Unknown platform!"
#endif
...
The problem is that when I try to include the file in my XCode project I got a warning (put them in comments)
...
#if TARGET_OS_TV //'TARGET_OS_TV' is not defined, evaluates to 0
#define UNITY_TVOS 1
#elif TARGET_OS_IOS //'TARGET_OS_IOS' is not defined, evaluates to 0
#define UNITY_IOS 1
#else
#define UNITY_OSX 1 //'UNITY_OSX' macro redefined
#endif
...
I tried to use #pragma warning(suppress: 4101) and a few similar approaches, but it doesn't help
UPD
...
#ifdef TARGET_OS_TV
#define UNITY_TVOS 1
#elif TARGET_OS_IOS //'TARGET_OS_IOS' is not defined, evaluates to 0
#define UNITY_IOS 1
#else
#define UNITY_OSX 1
#endif
...
Using ifdef helps to get rid of the first warning, but the second one is still in place
UPD2
...
#ifdef TARGET_OS_TV
#define UNITY_TVOS 1
#elifdef TARGET_OS_IOS //Invalid preprocessing directive
#define UNITY_IOS 1
#else
#define UNITY_OSX 1
#endif
...
You should not use #if to test an undefined macro. The warning implies that you should use #ifdef instead.
You may not define a previously defined macro. You could first undefined the old definition, but that's rarely a good idea.
Using ifdef helps to get rid of the first warning, but the second one is still in place
In c++23 you will be able to use #elifdef instead.
Otherwise, you can use #elif defined(the_macro).

error C1189: #error : Please define your platform

I have this err when I build on visual studio ultimate2010, can you have me fix this error, thanks so much!
Error 1 error C1189: #error : Please define your
platform. d:\dzz\src\flexengine\fxcore\platform.h 28 1 battleserver
#pragma once
// 平台定义
#if defined __APPLE__
#include "AvailabilityMacros.h"
#include "TargetConditionals.h"
#if TARGET_OS_IPHONE
/* if compiling for iPhone */
#define PLATFORM_IPHONE 1
#else
#define PLATFORM_MACOSX 1
#endif
#elif TARGET_OS_IPHONE
#define PLATFORM_IPHONE 1
#elif ANDROID
#define PLATFORM_ANDROID 1
#elif _WINDOWS
#define PLATFORM_WINDOWS 1
#elif (defined(__linux__))
#define PLATFORM_LINUX 1
#elif (defined(unix))
#define PLATFORM_UNIX 1
#else
#error Please define your platform.
#endif
// 64位检测
#if defined(__x86_64__) || defined(_M_X64) || defined(__LP64__) || defined(__POWERPC64__) || defined( _WIN64 )
#define PLATFORM_64 1
#elif defined(__i386__) || defined(_M_IX86) || defined(_M_PPC) || defined(__LP32__) || defined(__POWERPC__) || IPHONE || ANDROID
#define PLATFORM_32 1
#else
#define PLATFORM_32 1
// #error Please define your platform.
#endif
// 是否支持异常
#if PLATFORM_WINDOWS
#define PLATFORM_EXCEPTIONS 1
#else
#define PLATFORM_EXCEPTIONS 0
#endif
// Platform specific include.
#if PLATFORM_WINDOWS
#include "platform_windows.h"
#elif PLATFORM_IPHONE
#include "platform_iphone.h"
#elif PLATFORM_MACOSX
#include "platform_macosx.h"
#elif PLATFORM_ANDROID
#include "platform_android.h"
#elif PLATFORM_LINUX
#include "platform_linux.h"
#else
#error Unknown platform.
#endif
It appears that this platform.h attempts to use pre-defined macros to detect the target platform. However, it tries to use _WINDOWS macro to detect windows, which is apparently a pre-processor macro supported by the Watcom compiler.
Your options:
Since the library that you use apparently only supports the watcom compiler on windows, you could use that instead of visual studio. If you choose to use VS, there may be other issues down the line that need to be fixed to support it besides this one.
You could work around the issue by defining the macro yourself.
You could fix the header to work with visual studio by replacing #elif _WINDOWS with #elif _WIN32. See macros pre-defined by visual studio.
I recommend going with the latter approach, however porting the library to VS may (or may not) involve a non-trivial amount of work.

What is the difference between VERIFY and ATLVERIFY

What is the difference between the C++ macro VERIFY() and ATLVERIFY() ? And which one is better to use for COM objects?
There is some difference in how the errors are reported. ATLVERIFY is defined as
#ifndef ATLVERIFY
#ifdef _DEBUG
#define ATLVERIFY(expr) ATLASSERT(expr)
#else
#define ATLVERIFY(expr) (expr)
#endif // DEBUG
#endif // ATLVERIFY
And ATLASSERT is
#ifndef ATLASSERT
#define ATLASSERT(expr) _ASSERTE(expr)
#endif // ATLASSERT
So it ends up in _ASSERTE (see https://msdn.microsoft.com/en-us/library/ezb1wyez.aspx )
While VERIFY is
#ifdef _DEBUG
#define VERIFY(f) ASSERT(f)
#else // _DEBUG
#define VERIFY(f) ((void)(f))
#endif // !_DEBUG
So it ends up in ASSERT (see https://msdn.microsoft.com/en-us/library/aa297139(v=vs.60).aspx )

Determining when cross compiling for 32 bit from 64-bit in preprocessor?

I used the answer from Determining 32 vs 64 bit in C++ to make this:
#ifndef AVUNA_CFG
#define AVUNA_CFG
#if _WIN32 || _WIN64
#if _WIN64
#define BIT64
#else
#define BIT32
#endif
#endif
// Check GCC
#if __GNUC__
#if __x86_64__ || __ppc64__
#define BIT64
#else
#define BIT32
#endif
#endif
#endif
However, this doesn't seem to work when specifying -m32 to GCC for cross compiling, so it always says BIT64. Is there any defines I can use for this purpose?
I ended up using an Eclipse-define because I have two different run configurations for 32/64-bit cross compile. Works well.