How to add macro Visual Studio 2019 - c++

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

Related

#define not recognised by library

I am building a library project and a test project to test this library on. I want the user (A.K.A. the test project) to be able to define some things for the library, but the #define is not recognized in my library and just throws an error.
**I am building the library project as static library, the build order is that the library builds first and then the test project since the project is dependent on the library.
In the test project I want the user to be able to define like this (before the include):
#define PUT_DEFINE_HERE
In the library I just check with:
#ifdef PUT_DEFINE_HERE
//Do some stuff
#endif
As stated before the library does not see the #define defined by the user. I tried some stuff with the build order but no luck so far, any ideas?
If you want the library to "accept" a user #define, the check on the presence of PUT_DEFINE_HERE
#ifdef PUT_DEFINE_HERE
//Do some stuff
#endif
must be done at the header lever, and only there
Secondly, the user project which includes the library header must #define PUT_DEFINE_HERE before including the library header.
Alternatively, if such a setting is incompatible with the library design, then the #define must be added at compile time, e.g.:
gcc -DPUT_DEFINE_HERE ...
In this case, be sure to compile every source with -DPUT_DEFINE_HERE, when relevant.

Preprocessor Definition Setting: Project Properties ->C/C++ -> Preprocessor

I have a Dll, which I need to statically Link.
This dll eg. DTE.dll is a Third party dll.
My application is facing problem in linking this dll Statically.
I wish to Link this dll ( DteStatic.lib which is newly sent by the third party).
There is a setting in the Project Properties ->C/C++ -> Preprocessor: where I need to add DTE_STATIC.
My question: How do I do Static Linking when I have a dte.lib file.
i think you can give the DteStatic.lib while linking , it will do static linking in case visual studio compilers .
The _ won't create any problem. In my project in Project Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions I have _LIB and _DEBUG.
Edit:
DTE_STATIC is likely some preprocessor definition specific for your library. I don't know anything about DTE_STATIC, but as an example let's look how _DEBUG definition could work.
You could have some code you want to compile only in debug mode. For example this code can write some debug info on the command line, but it's not a good idea to show this info to your customer in release build. This code could look like this:
int x = 4;
x *= -8 << 2;
// ...
#ifdef _DEBUG
printf("DEBUG INFO: %d\n", x);
#endif
// ...
If _DEBUG is not defined the line between if is just omitted. If you set _DEBUG in Preprocessor Definition that line is compiled and executed.
Note: Setting preprocessor definition in Visual Studio is equivalent simply writing #define _DEBUG on the top of your header files.

VS 2015 compiler can't see a C variable

I'm writing a c++ project with MS Visual Studio 2015. In my project I have to call some variables declared and defined in a C file.
As you can see below, the variables are enclosed within the extern "C" structure:
#if defined __cplusplus
extern "C" {
#endif
GainType MYDLL_API BestCost; /* Cost of the tour in BestTour */
int MYDLL_API *BestTour; /* Table containing best tour found */
double MYDLL_API LowerBound; /* Lower bound found by the ascent */
#if defined __cplusplus
}
#endif
But, however, the compiler isn't able to resolve these three variables.
I've searched everywhere, i've tried all the proposed solutions, but it still doesn't work.
How can I solve it?
Thanks to everybody!
This looks like the c code is in a DLL
For the compiler to find this.
Need to define MYDLL_API as __declspec(dllimport) for C++
Need to link with .lib file
Seems that the symbols come from a DLL library. Then you need to add the import library (*.lib) of the DLL to the list of libraries the application is linked with.
In Visual Studio options, this can be done under project properties, Linker / Input / Additional Dependencies (add the lib name there).
Also make sure that the path to the *.lib is set in Linker / General / Additional Library Directories.

Where is WIN32 defined, and how can I include this definition in my project?

I am including a third party header and source file into my project.
At the top of the header there is this:
#if defined(WIN32) || defined(WIN16)
#ifndef MSDOS
#define MSDOS
#endif
#endif
#include <stdio.h>
#include <stdlib.h>
#ifndef MSDOS
#include <unistd.h>
#endif
#include "des.h"
The problem is that #if defined(WIN32) fails and the compilation fails when trying to #include unistd.h which I don't want to do.
I have third party project that works with this header file i.e. WIN32 is defined and it doesn't try to include In Visual Studio I did "Go To Definition" on "WIN32" and was taken to the following definition in WinDefs.h.
#define WIN32
I'm not sure this is where its getting WIN32 definition from, as the third party project does not appear to include "WinDefs.h".
So my problem is, how can I get WIN32 to be defined in my current new project?
Depends on your project setup. WIN32 is defined inside the windows header files, but you can pass it to the compiler as well ("-DWIN32" for gcc for example). Try it and see whether it compiles.
Visual Studio has the built-in define _WIN32. mingw-gcc has WIN32 and _WIN32 built-in so the project was likely tested using gcc. You might add
#if defined(_WIN32) && !defined(WIN32)
#define WIN32
#endif
or just add a -DWIN32 to the CFLAGS.
Check your includes. I am guessing that the third party header is included prior to the windows.h. So, in your main.cpp or equal it should be
#include <windows.h> // this will also include windefs.h
#include <thirdParty.h>
and not the other way around.
Hope that helps.
You can simply include the windows header files (windows.h) before including the third party header - as you already found out WIN32 is defined there but technicaly it could be defined anywhere (so if the third party project is not including the windows headers check if it's being defined in the compiler project settins directly).
BTW there is also a _WIN32 define that is set by the compiler, it's possibly a better idea to look for this define if checking if the code is being compiled under windows;
For those seeking answers to the
where is WIN32 defined
part of the questions, I've found it defined in:
minwindef.h
ole2.h
Note, I have no confidence that these are the only places it's defined. I expect there are probably other files where it's defined. Nevertheless, I thought this might help some people.
Some WIN32 defined in the compiler . Just like this,If you use the gcc for windows , WIN32 is defined . If you use the gcc for linux , WIN32 is not defined :)
So , the macros is a switch. You can define it to use somethine , and not define it to unuse something.

Using preprocessor directives to define command line options

If I wanted to add, let's say, a new .lib to the build only if a particular #define was set, how would I do that?
In the MSVC++ 2008 "Property Pages", you would simply add: Config Properties -> Linker -> Input -> Additional Dependencies, but I would like it if something like #define COMPILE_WITH_DETOURS was set, then the particular library would be added to the dependencies, otherwise it would be removed.
You can set some linker options by using #pragma comment in one of your source files.
For example, to link against a 'detours.lib' library only if COMPILE_WITH_DETOURS is defined, you can use:
#ifdef COMPILE_WITH_DETOURS
# pragma comment(lib, "detours.lib")
#endif
(this is specific to Microsoft Visual C++ and is not portable)