Using preprocessor directives to define command line options - c++

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)

Related

C++ conditional link with preprocessor

I have a MSVC C++ project, I am conditionally compiling parts of the source code by passing specifing Preprocessor Defintions in the C++/Preprocessor section of the Project Properties.
What I would also like to do is conditionally link with libraries based on the preprocessor definitions, how do I achieve this?
For example in my project if CLSOPENLDV is defined I want to exclude:
nodetalk32_vcpp.obj
and include:
ldv32.lib
And when it isn't defined I want to do the opposite.
You probably need this:
#ifdef SOME_MACRO
#pragma comment( lib, "ldv32" )
#endif
This is Microsoft specific, it probably won't work with gcc, clang or other compilers.
For excluding nodetalk32_vcpp.obj the only thing you can do that comes into my mind is:
#ifndef SOME_MACRO
// content of nodetalk32_vcpp.cpp
#endif

Auto link static libraries from header file

I'm writing a library for in-house use and I want to automate the linking of static library (.lib) files so users don't have to know all the libraries they need. We use Visual Studio 2015 so I've knocked something up using pragma which seems to work but I'm getting a lot or warnings, which I presume are caused by what I'm doing.
Warning LNK4221 This object file does not define any previously
undefined public symbols, so it will not be used by any link operation
that consumes this library
I include this code in all the public facing interface header files and extend it to include internal and external libraries necessary.
#ifndef GFXK_LIBS_NET_IMPORT__
#define GFXK_LIBS_NET_IMPORT__
#ifdef _DEBUG
#ifdef WIN32
#pragma comment( lib, "gfxk_net_Debug_Win32-v140.lib" )
#else
#pragma comment( lib, "gfxk_net_Debug_x64-v140.lib" )
#endif
#else
#ifdef WIN32
#pragma comment( lib, "gfxk_net_Release_Win32-v140" )
#else
#pragma comment( lib, "gfxk_net_Release_x64-v140" )
#endif
#endif /*_DEBUG*/
#endif /*GFXK_LIBS_NET_IMPORT__*/
My question is how do I do this nicely so that I can remove this hack job. I'm looking for something similar to what Boost does with it's auto linking, I can't find out how to do this. I'm not too worried about cross platform as library targets only Windows at this time.
You can specify .libs path in your project settings Configuration Properties -> Linker -> Input -> Additional dependencies. You may specify different settings for different configurations and platforms. That would be a clearer way.
But the warning won't appear after you add functions to your .lib and use them in your project. Warning is just saying that this pragma-lib statement is pointless as no functions are really imported.

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.

Working with lib files in VS2013

I created a lib file called StackExample.lib. There are functions and objects described in Stack.hpp and Example.hpp.
I want to have an easy time importing my library so I wrote another file called StackExample.hpp.
This file contains:
#pragma once
#pragma comment(lib, "StackExample.lib")
#include "Stack.hpp"
#include "Example.hpp"
Unfortunately I can no longer compile StackExample.lib because it can not import itself.
Is there a precompiler statement that I can use so that all importing programs load the lib but my lib would ignore that line?
#ifndef __STACK_EXAMPLE_INTERNAL
#pragma comment(lib, "StackExample.lib")
#endif
and then right click on your library project, select Properties => Configuration Properties => C/C++ => Preprocessor
Add __STACK_EXAMPLE_INTERNAL into "Preprocessor Definitions".
This way your library will define this definition and client's (hopefully) not.

What does "#pragma comment" mean?

What does #pragma comment mean in the following?
#pragma comment(lib, "kernel32")
#pragma comment(lib, "user32")
#pragma comment is a compiler directive which indicates Visual C++ to leave a comment in the generated object file. The comment can then be read by the linker when it processes object files.
#pragma comment(lib, libname) tells the linker to add the 'libname' library to the list of library dependencies, as if you had added it in the project properties at Linker->Input->Additional dependencies
See #pragma comment on microsoft.com
I've always called them "compiler directives." They direct the compiler to do things, branching, including libs like shown above, disabling specific errors etc., during the compilation phase.
Compiler companies usually create their own extensions to facilitate their features. For example, (I believe) Microsoft started the "#pragma once" deal and it was only in MS products, now I'm not so sure.
Pragma Directives It includes "#pragma comment" in the table you'll see.
HTH
I suspect GCC, for example, has their own set of #pragma's.
The answers and the documentation provided by MSDN is the best, but I would like to add one typical case that I use a lot which requires the use of #pragma comment to send a command to the linker at link time for example
#pragma comment(linker,"/ENTRY:Entry")
tell the linker to change the entry point form WinMain() to Entry() after that the CRTStartup going to transfer controll to Entry()
These link in the libraries selected in MSVC++.
Pragma directives specify operating system or machine specific (x86 or x64 etc) compiler options. There are several options available. Details can be found in https://msdn.microsoft.com/en-us/library/d9x1s805.aspx
#pragma comment( comment-type [,"commentstring"] ) has this format.
Refer https://msdn.microsoft.com/en-us/library/7f0aews7.aspx for details about different comment-type.
#pragma comment(lib, "kernel32")
#pragma comment(lib, "user32")
The above lines of code includes the library names (or path) that need to be searched by the linker. These details are included as part of the library-search record in the object
file.
So, in this case kernel.lib and user32.lib are searched by the linker and included in the final executable.