How to exclude a file for a compilation config? - visual-studio

I have a project that can either be built as an DLL or an EXE file, to be built as an .EXE file, a Main.cpp shall be included in the project.
I've set the Debug and Release configuration to build it as EXE file.
Now I'd like to set DebugDLL and ReleaseDLL configuration, (and other necessary adjustment), so that such Main.cpp will be excluded during compilation, and only generate DLL file?

we don't in fact exclude source file because we don't include them but we exclude headers and include them.
to exclude some code from source file consider using "conditional compilation"
#ifdef COMPILING_DLL
// ... dll code
#elif defined _CONSOLE
// .... console coe
#elif defined _WIN32
// ... win32 code
#endif

Related

Set path of Header files to avoid using relative paths (VS2015/c++)

H, i'm trying to set my project up so that i can avoid using relative paths for my header files in my project. The reason is that the project is multi-platform and I would like to avoid restructuring each use dependent on which system it's compiled on.
Currently, the header files do not use the correct path way and the only way i can use the includes is if i set relative paths to the files.
E.G.
for my file render.h i want to use:
#include "math/matrix.h" <--- this doesn't work
but
#include "../math/matrix.h" <--this works
What would i be doing incorrectly here for setting up the project?
in the Properties page, i have set up the following
VC++ Directories -> Include Directories -> C:\Game\math
C++ -> General -> Additional Include Directories -> C:\Game\math
If i right click on the .cpp files and go to properties, i have the C\C++ options but the Headers do not.
If your file resides in C:\Game\math\matrix.h, then an Include Directory of C:\Game\math and #include directive of "math/matrix.h", would produce a concatenated result of C:\Game\math\math/matrix.h. You simply need to change your Include Directory to be C:\Game (or your #include to be only "matrix.h").
Also, generally you set include directories per-project, not per source (.cpp) file. The reason that the header files do not have C++ compilation options is that they are not compiled - only the sources are compiled.

How to use a define in a header file of a Cmake generated library?

I have an Xcode project "App" which references another Xcode project "Lib".
The "Lib" project is generated by Cmake. I use "add_definitions(-DMYDEFINE)" to set a define. This define is used in a .h and a .cpp file in my lib project.
The problem is the header (.h) file. If I use it from within the Lib project, my define is set. If I use the file from my App project, the define is not set (header file of lib is in my include path).
This set/not set happens at the same time, although the header file is guarded with "#pragma once".
I need this define in the header file. Is there a way to organize things such that I can set the define only with Cmake in the Lib project?

#include file found in debug build but not in release build

I have a C++ (Brew C++) project in MVSC++ 10 which compiles in debug build but not in release build, but I cannot fathom out why not.
The problem is that header files within a certain directory cannot be found, the directory structure is:
.mak
.sln
etc.
/inc/A/m.h
/inc/B/n.h
/inc/C/o.h
/inc/util/html/a.h
/inc/util/html/b.h
/src/A/
/src/B/
/src/C/
/src/util/html/
i.e. the main project files are at the root directory and the header and source files distributed into sub folders.
a.h and b.h can be found in debug build but not found in release build.
Yet the .mak file contains:
....
INCDIRS := ....
$(ProjectDir)inc/A \
$(ProjectDir)inc/B \
$(ProjectDir)inc/util/html
So even though /inc/util/html is specified as an include directory in the .mak the project cannot find it - but only in release build.
There is no conditional behaviour between debug and relase builds that I can see - the header files are included within the source code irrespective of build configuration. And the above snippet is the only spot in the .mak file wehre the include directories are specified.
So why should there be a difference between the debug builds and release builds?
Any ideas?
Thanks

CMake: How to use different ADD_EXECUTABLE for debug build?

I'd like to build my application such that debug mode is a console application and release mode is a Win32 application. According to the documentation I need to add WIN32 to add_executable depending on whether I want a console application or not.
Because I'm using Visual Studio, I can't use CMAKE_BUILD_TYPE (the generated project contains multiple configurations). How can I tell CMAKE to use WIN32 for release builds and omit it for debug builds?
Quoting http://www.cmake.org/Wiki/VSConfigSpecificSettings
if(WIN32)
set_target_properties(WindowApplicationExample PROPERTIES LINK_FLAGS_DEBUG "/SUBSYSTEM:CONSOLE")
set_target_properties(WindowApplicationExample PROPERTIES COMPILE_DEFINITIONS_DEBUG "_CONSOLE")
set_target_properties(WindowApplicationExample PROPERTIES LINK_FLAGS_RELWITHDEBINFO "/SUBSYSTEM:CONSOLE")
set_target_properties(WindowApplicationExample PROPERTIES COMPILE_DEFINITIONS_RELWITHDEBINFO "_CONSOLE")
set_target_properties(WindowApplicationExample PROPERTIES LINK_FLAGS_RELEASE "/SUBSYSTEM:windows")
set_target_properties(WindowApplicationExample PROPERTIES LINK_FLAGS_MINSIZEREL "/SUBSYSTEM:windows")
endif(WIN32)
UPDATE: This feature is broken in recent versions due to a bug. One workaround I've found is to specify "/SUBSYSTEM:windows" instead of "/SUBSYSTEM:WINDOWS". That seems to work for some reason.
Dunno if this bug has been fixed in CMake yet. I'm using VC++ 2010 express and CMake v2.8.10.1 (which is currently the latest release) and I'm still having the exact same problem.
A working solution was provided here: modify your source code (e.g. main.cpp/main.c) by adding:
#ifndef NDEBUG
#pragma comment(linker, "/SUBSYSTEM:CONSOLE")
#endif
Alternatively, you could add the linker flag "/SUBSYSTEM:WINDOWS" to the release-mode build. I'm using this definition which seems to work:
#ifdef _MSC_VER
# ifdef NDEBUG
# pragma comment(linker, "/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup")
# else
# pragma comment(linker, "/SUBSYSTEM:CONSOLE")
# endif
#endif
Use the entry-point setting in order to avoid linker errors in case you've defined:
int main(int argc, char* argv[]) { ... }

How to reference .cpp files without including them into the solution in Visual Studio?

I'm trying to compile this code:
extern "C"
{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
#include <luabind/luabind.hpp>
#include<iostream>
int main(){
lua_State*pL=lua_open();
luabind::open(pL);
lua_close(pL);
return 0;
}
But I don't have a .lib of luabind, so I use the source with the .h/.cpp files.
The way I do it is by adding the directories to include, but I get a link error.
The only way I can compile is by adding the .cpp files as existing elements, but the solution tree gets messy with the additional files.
Can somebody tell me if there's a way to add the directory of the additional .cpp files in the solution's properties?
Thanks
Compile the lua cpp files into a static library. Add the directory where you put those under "linker | input | additional library directories".
You need to tell the linker where to find the functions referenced by the .h files (the .lib file, typically).

Resources