Visual Studio 2008 Generate Manifest from Macros - c++

I have created a C++ application in Visual Studio 2008. Now I would like to add properties to the manifest such as the application name and version. Both of these properties are defined within the C++ code as pre-processor macros. Is there anyway for me to populate the manifest from these MACRO values? For example:
#define APP_NAME "MyAppName"
#define APP_VERSION "MyAppVersion"
Is there some #pragma statement that I can use to add this information to the manifest? Something like:
#pragma comment(linker, "/MANIFEST:name=" APP_NAME " version=" APP_VERSION)

Related

How to add macro Visual Studio 2019

I was trying to make a program to sniff packets and went to libtins
If you are using a static build of libtins on Windows, then you have link your application with tins.lib. You also need to add this macro definition to your project:
TINS_STATIC
What does it mean? It even does not have a value. Can some one help me how to add this in visual studio?
Does it mean something like
#define TINS_STATIC
See this answer for a full guide for using libtins with Visual Studio.
Using #define TINS_STATIC (before including any libtins headers) would work. Alternatively you could add TINS_STATIC in the project settings under C/C++ > Preprocessor > Preprocessor definitions.
It does not need to have a value, because the libtins header only checks if the symbol is defined, not what value it has (reference):
// If libtins was built into a shared library
#if defined(_WIN32) && !defined(TINS_STATIC)
...
#endif // _WIN32 && !TINS_STATIC

Visual Studio Code C++ remove 'define' in c_cpp_properties.json

I know it is possible to add defines for Visual Studio Code in c_cpp_properties.json and I manually define __GNUC__ for my code, but is it possible to undo/remove defines that Visual Studio Code assumes for itself? For example if I set intelliSenseMode to clang-x64 the macro __clang__ is defined which completely destroys my intellisense because I don't have appropriate include files for libraries I use and include selection for __clang__ happens before __GNUC__. Same for msvc-x64 value. If I manually #undef __clang__ in my include files then everything is perfect.
Is it possible to undo macro in Visual Studio Code configuration?
Yes
First, create a header file called, say, vscode-preinclude.h. Put it anywhere; I'll assume it is in the workspace folder (the one that also has .vscode in it). Inside that file use #undef to undefine the symbols you need turned off. Example:
#undef __clang__
Next, use the Command Palette (Ctrl+Shift+P) and open "C/C++: Edit Configurations (UI)". Go down to the bottom and open "Advanced Settings". Scroll down to "Forced include", and add a line:
${workspaceFolder}/vscode-preinclude.h
That's it!
Troubleshooting
If it doesn't work, take a look at the output of the "C/C++: Log Diagnostics" command. It should show something like:
Forced Includes:
D:\WRK\LEARN\VSCODE\CPPHELLO\VSCODE-PREINCLUDE.H
in its output.
If you don't want the C++ extension to auto-configure your system includes & defines, you can set "compilerPath": "" for your configuration in your c_cpp_properties.json and the extension will stop auto-configuring you.

Where does <LIBNAME>_EXPORTS comes from?

I have seen many c++ header file under MSVC environment like this:
#ifdef somelib_EXPORTS
#define DLLEXPORTS __declspec(dllexport)
#else
#define DLLEXPORTS __declspec(dllimport)
#endif
Where does the <libname>_EXPORTS convention come from ?
Is it a Visual Studio's default macro ? Or is it generated by CMake ?
I can't find any document about this convention.
Where does the _EXPORTS convention come from ?
This is general coding guideline that helps with making sure that a .h file can be #included while building a DLL and by the users of the DLL.
The name DLLEXPORTS is project specific. It can be generated by CMake, qmake, or manually. I don't think Visual Studio can generate them, at least I haven't seen that.
If you have 10 DLLs in your project, you will end up using 10 such macros. For example, you might have UTILITY_DLL_EXPORT, MESSAGING_DLL_EXPORT, KERNEL_DLL_EXPORT, etc.

Multiple Version Resources

I am writing several C++ libraries inside of visual studio. I know I can use the Version Resource to assign DLL's a version, product name, and so on but is there a way that I can swap out different Resource scripts depending on my configuration settings. For instance, say I am compiling for x86 I want the product name to be x86, likewise for x64 I want the product name to be x64.
You can use #ifdefs in resource files. But using #ifdefs directly in he resource files, can sometimes screw with the IDE's resource editor (it has happened for me in Visual Studio 2008). I would therefore create two resource files (e.g. resource-x86.rc and resource-x64.rc) and include them in the main rc file like this:
#ifdef X86
#include "resource-x86.rc"
#elif X64
#include "resource-x64.rc"
#else
#error Unsupported platform!
#endif
copied from here
--
Open your project in Visual Studio
Right click on resource script file (e.g. app.rc) and select "Properties"
At the top of the property page, select one platform like "Win32" or "x64".
In the left menu bar, select [Configuration Properties] / [Resources] / [General].
In the "Preprocessor Definitions" field, add "WIN32" for "Win32" platform and "WIN64" for "x64" platform. The field value will become "WINXX;_UNICODE;UNICODE". (XX will be 32 or 64)
Click OK to close the window.
Right click on resource script file (e.g. app.rc) and select "View Code".
In the code editor, add #ifdef and #elif to conditionally include resources when compiling. Use "WIN32" and "WIN64" preprocessor definitions that we defined just now.
Here is a sample code:
--------------------------------
#ifdef WIN32
IDB_BITMAP1 BITMAP "bitmap1.bmp"
IDB_BITMAP2 BITMAP "bitmap2.bmp"
#elif WIN64
IDR_TOOLBAR1 BITMAP "toolbar1.bmp"
IDI_ICON1 ICON "icon1.ico"
#endif
--------------------------------
Save the resource script file and compile the project in different platforms.

How do I setup visual studio to register some #defines globally?

What I mean is, in each of my source files I have to insert #define NOGDI to stop windows.h from including GDI defines (since it's BITMAP define conflicts with mine).
e.g.
#define NOGDI
#include <windows.h>
Unfortunately, I have to do this in every seperate source file which includes windows.h, which I do not want to do.
I am using Visual Studio 2005, is there any way I can set it to #define something globally? (i.e. in all of the source files).
Project Settings -> C/C++ -> Preprocessor -> Preprocessor definitions
Here you can define symbols that are applied globally to all source code in your project.
I had set up the needed define for whole project, as described, in
Project Settings -> C/C++ -> Preprocessor -> Preprocessor definitions
But nevertheless this didn't work!
Eventually it turned out that problem was in checked NoInherit checkbox,
"Inherit from parent or project defaults"
In defines' line of Preprocessor Definitions Dialog it's seen as:
WIN32;_DEBUG;_WINDOWS;_MBCS;$(NoInherit)
Checked the thing back and the define finally recognized.
For VS2015 I edited the .vcxproj file and added a section such as this:
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemDefinitionGroup>
<ClCompile>
<PreprocessorDefinitions>MY_VARIABLE=SOMETHING</PreprocessorDefinitions>