visual studio solution configuration value in C++ - 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

Related

Trying to move a MSVC2008 project to MSVC2017

I'm trying to move this project form VisualStudio 2008 to VisualStudio 2017. This project is a C++ project and when I try to compile it there are a lot of errors:
Errors are:
the source file can not be opened (E1696).
the global scope does not have... (E0282).
the identifier ... is not defined (E0020).
The solution file of the project is in the folder kex3_anubis/msvc/
The project is open source.
# EDIT
I have lowered the number of errors to 17, but there are 126 warnings. What I have done is to select Project -> redirect projects (2nd option of project menu) and select Windows SDK version to 10.0.15063.0. I don't know if this is a good step or is not part of the solution, because it's the first time that I'm trying to move a project from different versions of VS.
The new errors are:
The declaration of ... hides the previous local statement (C4456)
Warning treated as error; no generated 'object' file (C2220)
The other errors have disappeared.
# EDIT 2
For the moment I applied this solution found on other question:
Go to project properties -> configurations properties -> C/C++ ->
treats warning as error -> No (/WX-).
Now I only have one error.
can not open the file '...\openal\lib\ eaxguid.lib' from anubis project
instead of
#include "math.h"
use
#include <math.h>
In all the standard includes

What does wxPuts do?

I'm trying to learn wxWidgets using this tutorial. It directs me to use a function called wxPuts() to put text in the console. My program compiles fine, but nothing shows up. I've searched for documentation to see exactly what wxPuts() is supposed to do so that I can get the settings right, but I'm having no luck.
Here is my code:
#include <cstdlib>
#include <wx\string.h>
int main()
{
wxPuts(wxT("A wxWidgets console application."));
return EXIT_SUCCESS;
}
What is wxPuts() supposed to do? I'm using Code::Blocks, but I also have Eclipse CDT and Microsoft Visual C++. How do I set up my IDE, so that wxPuts() gives the correct output?
I had to # include <wx/crt.h>
to get wxPuts to work using Code Blocks 17.12, wxWidgets 3.1.2 on Windows 7 Pro.
I received the advice to include crt.h from:
Why doesn't Clion recognise WxPuts?
Hope this helps someone else.
I feel kinda dumb. It turns out I wasn't waiting long enough for the text to pop up. For some reason it takes a very long time. My bad.
This function of wx/string.h library. It's show message in console. In order to work correctly. This should include some additional information in project properties.
Right Click to Project Properties
Built -> C++ Compiler -> in Additional Options add "wx-config --cxxflags" without quotes
Built -> Linker -> in Additional Options add "wx-config --libs" without quotes
Then it should works fine.

How to set predefined macros in Code::Blocks

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" ...

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

C / C++ : Portable way to detect debug / release?

Is there a standardized (e.g. implemented by all major compilers) #define that will allow me to distinguish between debug and release builds?
if believe
#ifdef NDEBUG
// nondebug
#else
// debug code
#endif
is the most portable.
But no compiler knows whether you are compiling debug or release, so this isn't automatic. But this one is used by assert.h in the c-runtime, so it's quite common. Visual Studio will set it, and I'm sure most other IDE's will as well.
Since there is no standard definition of debug or release, there isn't a way to do this. I can think of at least four different things that could be meant, and they can all be changed independently. Only two can be tested from within the code.
Compiler optimization level
Debugging symbols included in binary (these can even be removed at a later date)
assert() enabled (NDEBUG not defined)
logging turned off
Edit: I misread the question and waffled off on a different tangent!!! Apologies...
The macro _NDEBUG is used on Linux as well as on Windows...
If the binary is built and you need to determine if the build was release/debug, you can get a hexadecimal dump, if you see loads of symbols in it that would be debugging information...for example, under Linux, using the strings utility. There is a version available for Windows by SysInternals, available here on technet. Release versions of binary executables would not have the strings representing different symbols...
strings some_binary
Hope this helps,
Best regards,
Tom.
Best I could come with is
#ifndef NDEBUG
// Production builds should set NDEBUG=1
#define NDEBUG false
#else
#define NDEBUG true
#endif
#ifndef DEBUG
#define DEBUG !NDEBUG
#endif
Then you can wrap your debug code in if(DEBUG) { ... }.