how to set v120xp in cmake with vs2013's NMake - visual-studio-2013

I'm using win7-32bit + cmake + vs2013's NMake.exe to build exe, I need the exe be able to run on WinXP, I know how to do that with vs2013 IDE(set the Platform-Toolset to v120xp), but I'm not using the IDE, I just use its NMake. This is how I generate the project file and exe:
build> cmake -G "NMake Makefiles" ..
build> nmake
Question 1: In the CMakeLists.txt, how to set it use v120xp?
Question 2: Is it necessary to build all static lib with the v120xp? Or just the exe?

Try setting CMAKE_GENERATOR_TOOLSET. It allows selecting the toolset for Genertors that support it. Generators that support Toolsets are Visual Studio and XCode.
Your call to CMake should look like this:
cmake -G "NMake Makefiles" -DCMAKE_GENERATOR_TOOLSET=v120_xp ..
Update 1: As pointed out in the comments NMake doesn't support Toolsets
Thes solution is to specify
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SUBSYSTEM:CONSOLE,5.01")
for console applications, or
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SUBSYSTEM:WINDOWS,5.01")
for windows applications.

Related

How to configure CMakeLists to use Windows command line compiler instead of Visual Studio

I would like to convert my project from a Visual Studio solution to build with CMake and compile it with Makefiles.
This is a 2-part question.
Right now the CMakeLists.txt is:
cmake_minimum_required(VERSION 3.13.0)
project(Project2015 CXX)
add_executable(Project Source/main.cpp)
When I run cmake .. out of the build directory, it generates *.vcxproj and *.sln files, but there is no Makefile. How can I change the CMakeLists file to generate a Makefile?
What is the command line equivalent compiler to gcc for windows? And how do I set this compiler as the target for CMake and the generated Makefile?
Reading about the build tools https://learn.microsoft.com/en-us/cpp/build/walkthrough-compile-a-c-program-on-the-command-line?view=vs-2019
Do I need to target the cl.exe compiler? Would this work with CMake and Makefiles?
I'm reading online that these command line flags will set the compiler, how can I add these to the CMakeLists.txt to be used automatically?
DCMAKE_C_COMPILER=cl
DCMAKE_C_COMPILER_FORCED=ON
DCMAKE_CXX_COMPILER=cl
DCMAKE_CXX_COMPILER_FORCED=ON
DCMAKE_BUILD_TYPE=Debug
DCMAKE_INSTALL_PREFIX=%CFITSIO_DIR%
G"NMake Makefiles"
You should use the build tool mode of CMake for builds from the command line.
After configuring your project for a 64bit build using Visual Studio 2019 e.g. with
cmake -S <sourcedir> -B <builddir> -G "Visual Studio 16 2019" -A x64
you would run
cmake --build <builddir> --target ALL_BUILD --config Release
For further options see here for an almost quiet build from the command line see here.
As suggested by #vre, you can run everything from the command line, while still using the Visual Studio generator. Just use CMake's command line build tools:
cmake ..
cmake --build . --config Release
This way, you don't have to open Visual Studio at all to build your libraries/executables.
Another option is to use Microsoft's nmake utility, which will generate NMake Makefiles. You can tell CMake to use this generator instead using this:
cmake -G"NMake Makefiles" ..
The full list of CMake generators you can choose from is listed here.
If you don't want to manually set the CMake generator in the command line, you can set it at the top of your CMakeLists.txt file:
set (CMAKE_GENERATOR "NMake Makefiles" CACHE INTERNAL "" FORCE)
It will be used on the second CMake configuration in this case, as the first run will use the system default generator. If you want CMake to use it on the first configuration, you can utilize the Preload.cmake procedure outlined in this answer.

How do I cross-compile scons (with gcc) on Windows with Visual Studio installed?

I have an existing makefile project that I am migrating to scons.
The makefile builds several Windows executables with gcc and g++.
However, I also have Visual Studio installed for C# development.
It appears that scons is trying to use the Visual Studio tools rather than the gcc ones:
cl /Fofoo\bar.o /c foo\bar.c /nologo -g -mno-ms-bitfields -fshort-enums -ftest-coverage -fprofile-arcs /D-DUNIT_TESTS /I. <more includes follow...>
cl : Command line warning D9002 : ignoring unknown option '-g'
I have read several answers and have tried adding:
env["CC"] = "gcc"
env["CXX"] = "g++"
env["LINK"] = "g++"
in my Sconstruct file. This has the effect of correctly changing the tool, but not the command syntax:
gcc /Fofoo\bar.o /c foo\bar.c /nologo -g -mno-ms-bitfields -fshort-enums -ftest-coverage -fprofile-arcs /D-DUNIT_TESTS /I. <more includes follow...>
gcc: error: /Fofoo\bar.o: No such file or directory
How can I ensure that scons uses my desired tools and also uses the correct syntax for the command line options (e.g. -I instead of /I)?
If I have to guess the issue your SConstruct is something like this:
env=Environment()
env["CC"] = "gcc"
env["CXX"] = "g++"
env["LINK"] = "g++"
env['CCFLAGS']='-mno-ms-bitfields -fshort-enums -ftest-coverage -fprofile-arcs'
env['CPPDEFINES']=['-DUNIT_TESTS']
env['CPPPATH'] = ['.']
Given that the default list of tools to configure on Windows is the following in order and it will stop configuring tools once it finds one of these and then it sets up the flags which should work for such tools.
c_compilers = ['msvc', 'mingw', 'gcc', 'intelc', 'icl', 'icc', 'cc', 'bcc32']
You'll need to explicitly list the tools you want initialized (And not allow SCons to add the default tools) and the PATH they will be found in. Also your CPPDEFINES should be ['UNIT_TESTS'] and not ['-DUNIT_TESTS'] SCons will add the appropriate flags. Note you may need to add other tools if you are using them in your build.
env=Environment(tools=[])
env.AppendENVPath('PATH', PATH_TO_YOUR_COMPILERS)
for tool in ['gcc','gnulink','ar']:
env.Tool(tool)
env['CCFLAGS']='-mno-ms-bitfields -fshort-enums -ftest-coverage -fprofile-arcs'
env['CPPDEFINES']=['UNIT_TESTS']
env['CPPPATH'] = ['.']

CMake and slow MSVC compilation

CMake generate project for MSVC 10 (2010) and build time are relativly low, ie. after I click build for given project it start compiling in like seconds, which is expected.
For Qt Creator I choose CodeBlocks - NMake Makefiles as CMake generator, set build configuration to use the same target as above. When I click build I see
cmake --build . --target name
and silence for like 2 minutes. ThenScanning dependencies for ... and 1,5 minutes more of silence.
To make things even worse - later compilation use only 1 thread. So compilation time is ... unacceptably long.
How should I configure project to achieve comparable, short compilation times as from under MSVC? I've tried Qt Creator 4.3 Beta and beside
CMake per default does not multiprocessor builds. So its up to you to force it to...
You can specify the /MP flag when you configure your project with cmake.
cmake -G "<MSVC-Generator>" -DCMAKE_CXX_FLAGS="/MP" <target_dir>
I do this for all my cmake projects in the file CMakeLists.txt itself so the client does not have to:
if(MSVC)
target_compile_options(<target> PRIVATE "/MP")
endif()
CMake calls the compiler with default parameters, that is, with a single thread build, even for MSVC. Your build in MSVS is faster because Visual Studio calls the compiler with the settings configured for multiprocessor build.
How to use multiprocessor build for CodeBlocks - NMake Makefiles or other cmake generators?
Add -jn option for gcc compiler:
cmake --build . --target name -- -j4
or /m for MSVC:
cmake --build . --target name -- /m
For MSVS 2017 or newer instead of /m you can use -m form. Also, you can specify the maximum number of concurrent processes to use when building-m:3.
NMake and NMake JOM are slow.
When I change to Ninja build system - compilation times are similar to those inside MSVC.

make without makefile after cmake

I try to use the c++ language bindings for the ev3dev lego brick: https://github.com/ddemidov/ev3dev-lang-cpp
The instruction is as follows:
mkdir build
cd build
cmake .. -DEV3DEV_PLATFORM=EV3
make
I am running windows and have cmake and mingw available. After running cmake it creates some files in the build directory. However: There is no makefile which could be picked of by make. So I am wondering how iam supposed to compile these bindings
On Windows, CMake generates a MSVC solution by default. Check for a .sln file in your build directory.
The instructions you linked are assuming a Unix-ish platform, where the default is to create Makefiles.
If you actually want Makefiles on Windows, add -G "Unix Makefiles" to the cmake line.
If you want to use MSVC as compiler but work on the command line, another option is -G "NMake Makefiles", and calling nmake after that.
Make sure to delete your build directory before trying to build a new generator target. CMake can be touchy about that.
Check cmake --help for a list of available options. (Especially the generator targets are platform-specific.)

FLANN compile (cygwin)

I'm trying to build FLANN libriary on windows.But I have cygwin installed.
> "C:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat"
> cd flann-x.y.z-src
> mkdir build
> cd build
> cmake ..
> nmake
and when I use cmake .. it seems it uses cygwin and gcc compiler.
and then nmake don't work.
Quite unclear what you want to achieve:
You can build FLANN in cygwin environment by cygwin's cmake and make and then use it.
Or you can follow FLANN installation instructions and build it with window's native cmake and C++ compiler and use it (simply do not use cygwin at all, use cmd.exe).
In case you want to use VS2008 C++ compiler, do
cmake -G "Visual Studio 9 2008" ..
cmake --build . --config Release

Resources