How to set predefined macros in Code::Blocks - c++

Is there a way to set some predefined Macros for my local installation of Code::Blocks.
To elaborate on that, basically I would like to have certain blocks compiled only at pc and not anyplace I send the code to. One way to achieve this is as follows:
#define MYPC
#ifdef MYPC
//do something
#else
// do something else
#endif
I was to achieve the same thing, but I don't want to include the line #define MYPC and woud like to add this somewhere in the IDE. I know how to do this in Visual Studio, and I think it also exists in Code::Blocks as well.
Thanks.

Project - Properties - Project's build options - Compiler Settings - #defines.
Edit. Example of #defines edit box:
CONSTANT1
CONSTANT2="0"
Gives the following command line:
g++ -DCONSTANT1 -DCONSTANT2="0" ...

Related

Visual Studio 2019 C++ cross-platform directives #if #else ignored

I am using Visual Studio 2019 (updated to date, running on Windows) to rewrite old program code in cross-platform C++ language (Windows & Linux).
In the code, I am using the pre-compilation directives #if, #else, #endif to toggle platform-specific blocks of code.
Example:
76 #ifdef WINDOWS_OS
77 errno_t success = fopen_s(&arq, logConfigFileName.str().c_str(), "rt");
78 #else
79 arq = fopen(logConfigFileName.str().c_str(), "rt");
80 #endif
In the Visual Studio editor, everything looks correct (see screenshot).
Project selected: Windows, Debug, x86.
Lines 1 and 2 appear normally (active and without error indication) and lines 3 to 5 appear in gray indicating they are disabled, that is, it appears as it should be.
However, when compiling I get the error:
Error C4996 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
error on line 79, line that should be ignored by the compiler.
How can I fix this, so that my code can be compiled for both platforms without problems ?
You don't need to create / define macros to identify the OS.
Compiler do it already. See C++ compiling on Windows and Linux: ifdef switch for the list.
Problem solved.
The problem was in the definition of the WINDOWS_OS flag, I had defined it in the code with the #define WINDOWS_OS statement.
The IDE recognized the instruction, but the preprocessor didn't.
The solution was to include the WINDOWS_OS flag in the list of definitions in: Project Properties -> Settings Properties -> C++ > Preprocessor - Preprocessor Settings.
To me this does not make sense, my everyday IDE is Rad Studio and in it if you set a compiler directive in the code it is valid for everything.
Thank you all.

How can I make compiler version specific ifdef?

I've got the problem that my program will compile with g++10.2 and c++11 activated through cmake. but it will not compile with arduino dues arm-none-eabi-g++.exe compiler which also has c++11. The failure occurs because of one line that needs to be added for the arm compiler, but when I add that line to g++10.2 it won't compile.
So I need an #ifdef or some alternative to activate and deactivate the line specific for the compiler.
Like Deumaudit said in the comments:
Try to use __arm__, __aarch64__ or __ARM_ARCH macro
You'll probably be ok if you use #ifdef __arm__ or even #if defined(__arm__) || defined(__aarch64__)
If you're planning to add more supported platforms to your program, it might be a good idea to define some macros when building for a specific platform. I have my own _MY_APP_ARM macro defined in my CMakeLists.txt:
if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm")
add_definitions(-D_MY_APP_ARM)
endif()
Which I can then use as #ifdef _MY_APP_ARM

visual studio solution configuration value in C++

I would like to know if there is a way to get the solution configuration name (debug, release, etc) in the program maybe in a #define ?
I found on the forum this post but the litle function doesn't work for me.
Thanks a lot
Use YOUR_FANCY_NAME="$(ConfigurationName)" in [Configuration Properties] -> [C/C++] -> [Preprocessor] -> [Preprocessor Definitions].
If you don't need the exact name and only want to determine if the current build is a debug build, you can use the preprocessor define _DEBUG:
#ifdef _DEBUG
// stuff
#endif

Eclipse CDT for Linux

I have written a program using C++11 features.
/* * test.cpp * * Created on: 05-Jul-2015 * Author: avirup */
#include<vector>
#include<iterator>
#include<iostream>
using namespace std;
int main() {
vector<int> v;
v.push_back(5);
v.push_back(7);
for(auto i=v.begin();i!=v.end();i++) {
cout<<*i<<endl;
}
for(auto i=v.cbegin();i!=v.cend();++i) {
cout<<*i<<endl;
}
}
The program is compiling correctly and showing results but the editor is showing but red lines below valid functions like cbegin() and cend() which are constant reference iterators. which is annoying. How to get rid of this?
Just for completeness since this has no answer and give an explanation.
To achieve compiling with C++ 11 (or another version) and Eclipse actually supporting it you need to do two things.
First the compiler flag needs to set so -std=c++11 or -std=c++0x is appended when calling g++ or whatever is used.
Open the properties for the properties for the project. Select it and right click ↦ Properties (or Alt+Enter for Windows users)
Go to C/C++ Build ↦ Settings, maybe select your preferred configuration, ↦ GCC C++ Compiler (or any other compiler you use) ↦ Dialect.
Select from the combo or write the flag to the Other dialect flags if not present in the combo (like -std=gnu++14 or -std=c++1z).
CDT will now call your compiler with -std=c++0x when compiling. Now to the part so CDT supports C++11 and does not show errors for missing types and such. My libstdc++ contains lines like
#if __cplusplus < 201103L
# include <bits/c++0x_warning.h>
#else // C++0x
that causes your errors and the actual type declarations/definitions are greyed out if you view them in the C/C++ editor. __cplusplus needs to be set correctly with #define __cplusplus 201103L so they will be parsed and indexed by CDT. This is also done via the project settings or can also be done for the whole workspace.
Go again to the project settings.
C/C++ General ↦ Preprocessor Include Paths, Macros etc., also maybe select your preferred configuration, ↦ tab Providers.
Select the entry for your compiler (for me it’s CDT GCC Built-in Compiler Settings MinGW.
Add -std=c++11 or -std=c++0x to the Command to get compiler specs textfield at any location.
Optional: Select Allocate console in the Console View and hit apply. You should now see something like #define __cplusplus 201103L in the console.
To set it for the whole workspace just check Use global provider shared between projects and click on Workspace Settings where an almost identical dialog opens.
I’m currently writing a plug-in that extends the new C/C++ project wizard where one can select the C++ version for the project that sets the compiler flag correctly and also the above setting for the indexer and some other stuff. But dunno if it will ever will be integrated into CDT or needs to be installed via plug-in. But it sure will be in https://www.cevelop.com in a few months.

D_WIN32_WINNT compiler warning with Boost

Not sure what to make of this error. Added -D_WIN32_WINNT=0x0501 to Visual Studio's "Command Line" options under Project Properties but it says it doesn't recognize it and the warning still appears.
I am also not sure how to add the Preprocessor Definition.
1>Please define _WIN32_WINNT or
_WIN32_WINDOWS appropriately. For example:
1>- add -D_WIN32_WINNT=0x0501
to the compiler command line; or
1>-
add _WIN32_WINNT=0x0501 to your
project's Preprocessor Definitions.
Add following line in your top source code.
#include <SDKDDKVer.h>
I think you're really close to getting this to work. John Dibling gave three ways you could do this and it looks like you tried the third solution, which was to "go in to your project's settings ... and under the Configuration Properties->C/C++->PreProcessor heading, add ;_WIN32_WINNT = 0x0501". You replied that you were still getting that error and provided the contents of your preprocessor settings, WIN32;_DEBUG;_CONSOLE;_WIN32_WINNT = 0x0501. I think you can solve this if you change _WIN32_WINNT = 0x0501 to _WIN32_WINNT=0x0501. When I tried the version with spaces, it did not eliminate the error, but removing the spaces did.
A few options.
1) If you have a main header file, like stdafx.h, you could add this:
#define _WIN32_WINNT 0x0501
Or you could add that anywhere you need it.
2) You can add -D _WIN32_WINNT=0x0501 (note the space)
3) Go to Project Properties > Configuration Properties > C/C++ > Proporcessor. Add ;_WIN32_WINNT=0x0501 to Preprocessor Definitions.
Personally, I choose #3 because there's no doubt about it being defined at the right time in the right translation units, and I'd rather have all the #defines in one place rather than some being in Preprocessor Defines and others in the advanced tab.
Put a space after the D
You should define the WIndow sversion you ant to target as many have suggested:
// Target Windows XP
#define _WIN32_WINNT 0x0501
The reasons you do not want to use the SDK version that happens to be installed are:
To have reproducible builds. You don't want to just pick up whatever happens to be installed as that can very for each person trying to build.
To be in control of your target platform. You don't want to target Windows 11 just because you happen to compile on Windows 11. As the programmer, you want to be in control of what is done.
To support the widest range of Windows versions. Users will be grateful that they can run your program on their older Windows version.
For Code Blocks here is how you do it.
Right click **Project Name** on your left >> Click 'Build Options' >> Select Debug or Release on your left >> Select 'Compiler Settings' Tab on the right >> Select #defines tab >> Then add the following line as it is:
_WIN32_WINNT=0x0501
>> Click Ok >> Close >> Right click **Project Name** again >> Re-build.
OP didn't ask about CMake, but google brought me here. If you're using CMake, try adding this to your (top-level) CMakeLists.txt:
if (WIN32)
add_definitions(-D_WIN32_WINNT=<myWindowsTarget>
endif()
I was interested in Windows 10, so myWindowsTarget was 0x0A00. Here's a full list of Windows Targets