Unexplainable error "Please use the /MD switch for _AFXDLL builds" - visual-studio-2010

I use VisualStudio2010 and CMake 2.8.12.1. I created a CMakeLists.txt for a MFC project. MFC capability was done by following lines in the CMake file:
add_definitions(-D_AFXDLL) #enables MFC
set(CMAKE_MFC_FLAG 2) #use shared MFC library
Furthermore the project will be build with MD as runtime library (default). But now I want my project to be build as MT (which also requires to specify static MFC library). So I replaced the lines above with:
add_definitions(-D_AFXDLL) #enables MFC
set(CMAKE_MFC_FLAG 1) #use static MFC library
set(CMAKE_CXX_FLAGS_RELEASE "/MT") #set release configuration to MT
set(CMAKE_CXX_FLAGS_DEBUG "/MTd") #set debug configuration to MTd
If I build my project now, I get error message:
fatal error C1189: #error : Please use the /MD switch for _AFXDLL builds
As far as I know this means that there's a mismatch between MT/MD on one side and static/shared MFC library on the other side. I checked the configuration in my project settings once again. Runtime library IS set to MT and MFC library IS set to static. So I do not understand the error. Does anybody know help? Did I miss something?
Regards,
Michael

_AFXDLL is never used with a static MFC build! _AFXDLL.
_AFXDLL is only set when the shared MFC builds are used.
Also static MFC build implies a static CRT.

Related

CMake: Compiler flags for each MSVC project type

I am developing an C++ project that need to set specific compiler flags for each Visual Studio (MSVC) project configurations. We know that the configurations that CMake creates by default are these: Debug, RelWithDebInfo, Release and MinSizeRel.
In my CMakeLists.txt I am trying to do something like this:
# For each one Visual Studio configuration
# Some specifics compiler flags for MSVC
# For all RELEASE based configurations (Debug and RelWithDebInfo)
set(MSVC_FLAGS_REL STRING "/Ox /Ob2 /Oi /Ot /GF /Zc:wchar_t /MT")
# For all DEBUG based configurations (Release and MinSizeRel)
set(MSVC_FLAGS_DBG STRING "/MTd /Od /Zi /RTC1 /Zc:wchar_t /GS")
add_compile_options($<$<CONFIG:Debug>:${MSVC_FLAGS_DBG}>$<$<CONFIG:RelWithDebInfo>:${MSVC_FLAGS_DBG}>$<$<CONFIG:Release>:${MSVC_FLAGS_REL}>$<$<CONFIG:MinSizeRel>:${MSVC_FLAGS_REL}>)
However, I try to build each one configurations I noticed that the compiler flags are not been successfully set. Also, I noticed a few posts that the Generator Expressions are sometimes inadvisable in some situations.
How should I properly set the compiler variables for Debug and Release based configurations?
A bit dated, possibly, but I used this to good effect back in the days:
if ( MSVC )
set_property( TARGET ... APPEND_STRING PROPERTY COMPILE_FLAGS <your set of flags> )
endif()
(My setup figured out the BUILD_TYPE and assembled the right set of flags based on that, instead of setting one set for Release and one for Debug just in case. This was pretty early in CMake's lifespan, so some of the more advanced plumbing just was not available yet.)

How to turn off a project for Debug config in CMake-generated Visual Studio solution

Right-click on the solution title in the Solution Explorer window, then go to Configuration Properties -> Configuration. The table appears, showing check-boxes, allowing to turn off/on a build of particular projects for certain configurations.
My solution and projects are generated with CMake.
Is it possible to turn off a particular project for Debug build configuration from CMakeLists.txt?
==
Background of a problem is failing build of Cython project for Debug config.
Release builds fine. CMake module was taken from this example on Github.
Debug config wants debug Python library python27_d.lib, that is forced by pyconfig.h. I use Anaconda python, which is missing this library.
Moreover, I don't need debug build of that project. I've unsuccessfully spent several hours, modifying CMakeLists.txt in various ways, trying to remove definition of _DEBUG macro from compiler command line. CLI parameter /D_DEBUG was absent in all dialogs with properties and "complete command line" listings, that Visual Studio has shown me. Nevertheless, something has always appended it.
So, I'd like to simply disable this project in Debug build for now.
This sets that check-box from the first part of the question to unchecked state:
set_property(TARGET <my Cython module>
PROPERTY EXCLUDE_FROM_DEFAULT_BUILD_DEBUG TRUE)
Now I wonder, where did compiler command line come from, because /D_DEBUG was absent in all dialogs with properties, that Visual Studio has shown me (second part of the question).
I am building this project in VS2013. Initially, that string /D_DEBUG was present in Project properties -> C/C++ -> Preprocessor -> Preprocessor definitions for the Debug configuration. Then I've added
string(REPLACE "/D_DEBUG" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
to my CMakeLists.txt file, building the Cython code, and that macro has disappeared from the Project properties.
Nevertheless, the project was still requiring python27_d.dll.
I've also added
#define _DEBUG
in one of files, and have got the following compiler warning
C:\projects\project\file.cpp(9): warning C4005: '_DEBUG' : macro redefinition
command-line arguments : see previous definition of '_DEBUG'

Visual Studio 2010 Configuration to run PCL Project

I have a problem when trying to debug a simple PCL (Point-Cloud-Library Application) application in MS Visual C++ 2010 that has to visualize a point cloud. (This example: http://pointclouds.org/documentation/tutorials/pcl_visualizer.php)
I setup the C++ project with all the include and library folders needed for PCL.
I add to the linker input the following dependencies:
openNI.lib
libboost_system-vc100-mt-gd-1_49.lib
libboost_filesystem-vc100-mt-gd-1_49.lib
libboost_thread-vc100-mt-gd-1_49.lib
libboost_date_time-vc100-mt-gd-1_49.lib
libboost_iostreams-vc100-mt-gd-1_49.lib
pcl_common_debug.lib pcl_apps_debug.lib
pcl_features_debug.lib
pcl_filters_debug.lib
pcl_io_debug.lib
pcl_io_ply_debug.lib
pcl_kdtree_debug.lib
pcl_keypoints_debug.lib
pcl_octree_debug.lib
pcl_registration_debug.lib
pcl_sample_consensus_debug.lib
pcl_search_debug.lib
pcl_segmentation_debug.lib
pcl_surface_debug.lib
pcl_tracking_debug.lib
pcl_visualization_debug.lib
vtkRendering-gd.lib
QVTK-gd.lib
vtkalglib-gd.lib
vtkCharts-gd.lib
vtkCommon-gd.lib
vtkDICOMParser-gd.lib
vtkexoIIc-gd.lib
vtkexpat-gd.lib
vtkFiltering-gd.lib
vtkfreetype-gd.lib
vtkftgl-gd.lib
vtkGenericFiltering-gd.lib
vtkGeovis-gd.lib
vtkGraphics-gd.lib
vtkhdf5-gd.lib
vtkHybrid-gd.lib
vtkImaging-gd.lib
vtkInfovis-gd.lib
vtkIO-gd.lib
vtkjpeg-gd.lib
vtklibxml2-gd.lib
vtkmetaio-gd.lib
vtkNetCDF_cxx-gd.lib
vtkNetCDF-gd.lib
vtkpng-gd.lib
vtkproj4-gd.lib
vtksqlite-gd.lib
vtksys-gd.lib
vtktiff-gd.lib
vtkverdict-gd.lib
vtkViews-gd.lib
vtkVolumeRendering-gd.lib
vtkWidgets-gd.lib
vtkzlib-gd.lib
OpenGL32.Lib
When i start debugging the IDE ends up with the following link error:
LINK : fatal error LNK1104: cannot open file 'libboost_thread-vc100-mt-sgd-1_49.lib'
The library "libboost_thread-vc100-mt-sgd-1_49.lib" is not contained in the set of boost library of the 3party folder of PCL. What am I doing wrong? Is there another path for VS2010 configuration for PCL programs?
Additional information:
I downloaded last version 1.6 of PCL, choosing the MSVC2010 version all-in-one.
I set up the code generation with /MTd (Multi-threaded debug)
I'm running the project with debug mode.
You already got libboost_thread-vc100-mt-gd-1_49.lib so enable your project to use dynamic linking in "configuration properties".
Another option is to download Boost and build it using static linking to generate the libboost_thread-vc100-mt-sgd-1_49.lib file. Don't forget to set the folder of "Linker" -> "Additional Library directories" to the location of the file.

error LNK2038 mismatch detected

I am trying to run a code using Geant4 (A physics library) and trying to compile it on VS2010.
It seems that some libraries of Geant4 have problem matching with my C++ libraries. In compilation I get a huge number of similar errors like the following:
libG4Tree.a(G4ASCIITree.o) : error LNK2038 : mismatch detected for
'_ITERATOR_DEBUG_LEVEL' : value '0' doesn't match value '2' in
Workshopexample1.OBJ
And get this error with more than 100 different .a files in my G4 libraries.
I really need the debug level to be off ('0'). Since otherwise G4 codes will take years to be run. So, I need to find where is this _ITERATOR_DEBUG_LEVEL defined in my VS2010 and set it to '0' to avoid mismatching. Can anybody help?
Cheers,
Payam
The error can be caused by mixing together debug builds and release builds in same executable or dll.
Release version of the static lib with /MDd instead of /MD, whereas the application is /MD in release. Setting the correct /MD in the static lib project solved the issue.
This is done in Project properties
Select Configuration Properties / C C++ / Code Generation in the tree
and the option Runtime Library set to the same on all your dependencies projects and application.
Search for _ITERATOR_DEBUG_LEVEL and _SECURE_SCL remove them or set them appropriately in all projects and sources and rebuild everything.
_ITERATOR_DEBUG_LEVEL = 0 // disabled (for release builds)
_ITERATOR_DEBUG_LEVEL = 1 // enabled (if _SECURE_SCL is defined)
_ITERATOR_DEBUG_LEVEL = 2 // enabled (for debug builds)
In short you are probably mixing release and debug dlls. Don't linked release dlls in debug or vice versa!

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[]) { ... }

Resources