I need to generate a C++ header file that describes the compiler used.
Traditionally we used the CMake command:
configure( ${PROJECT_SOURCE_DIR}/configure.h.in ${PROJECT_BINARY_DIR}/configure.h )
which replaces all string sandwiched by "#" (for example #cxx_compiler_name#) with the value of that variable in the cmake build system.
We have been given the requirement to face out CMake, so is there something equivalent in Visual Studio. I'd like to populate the header file with some of the values in the Visual Studio macros.
You can add your configure.h.in file to the project and set a custom build for it that will run perl or sed and replace whatever needed. Don't forget to add configure.h to output files property so Visual Studio can figure out the dependencies and "build" configure.h.in before other sources that use configure.h.
Related
I use CMake to configure and generate makefiles for a Microsoft MPI project that I build and run from Visual Studio 2017. In order to run the project, I need to modify the VS solution configuration settings. Under Configuration Settings->Debugging, I want to specify "Command" and "Command Arguments" from the CMakeLists.txt. I can do this manually but I want to set it up from the CMakeLists.txt. Are there commands to do this?
CMake 3.12 introduced two new target properties for that purpose: VS_DEBUGGER_COMMAND and VS_DEBUGGER_COMMAND_ARGUMENTS. Set these properties in the following way:
set_target_properties(targetName PROPERTIES
VS_DEBUGGER_COMMAND "debug_command"
VS_DEBUGGER_COMMAND_ARGUMENTS "debug_arguments")
for anyone look for a solution on visual studio code with cmake extension, here is a solution:
change program field in launch.json to ${command:cmake.launchTargetPath},
launch.json
ref:
link
I'm trying figure out how to compile C++ code from an executable during runtime using Visual Studio compiler under Windows.
I'll be using Visual Studio IDE to build main project into an executable and use CreateProcess to compile other C++ files and create a DLL to later load/use/unload this DLL.
I understand that one way of doing this requires setting environment variables(mainly PATH, INCLUDE and LIB) and there's a .bat file called "vcvarsall.bat" which does this.
The part I'm stuck with is the argument(s) passed to this batch file. I see that first argument is the platform with some of the options being x86, amd64, arm, etc. But how do I programmatically figure out which one of these arguments I should be using considering main executable could've been built with any one of these?
You can prepare a regular solutionfor this purpose, containing one project with a single file, and use it to compile your file easily.
Now, all you need is to reame your file to the file name in the project and compile a solution with command line. Alternatively, you can also edit the project and replace the existing filename with your file name.
To do so you need to resolve the environment variable %DevEnvDir% and run the folowing command with the platform name (x64, win32 etc.) and configuration name(Release or Debug)
like this:
%DevEnvDir%\devenv.com \path\to\yoursolution.sln /ReBuild "Release|x64"
In my vs2013 project file, I have "Program database file name" (from Project Properties->C/C++->Output Files) to $(IntDir)vc$(PlatformToolsetVersion).pdb.
I know how to do this manualy, but for cmake i don't know.
How can I set this property from cmake?
You can use target properties COMPILE_PDB_NAME for the name and COMPILE_PDB_OUTPUT_DIRECTORY for the directory.
If you want to use the Visual Studio variables, you can try to use it directly in the variables above. If that's not working, a fallback is always to add the corresponding compiler options. In you case /Fd:
if (MSVC)
add_compile_options("/Fd\"$(IntDir)vc$(PlatformToolsetVersion).pdb\"")
endif()
or with generator expressions:
add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/Fd\"$(IntDir)vc$(PlatformToolsetVersion).pdb\">")
I'm trying to take a couple of projects normally compiled on Windows with Microsoft C++ and compile them with clang instead.
On the upside, there exists clang-cl.exe which is designed to be a drop-in replacement for cl.exe. However, even when I copy clang-cl.exe into the current directory as cl.exe, msbuild still in some cases calls Microsoft's cl.exe.
Is there a way to tell msbuild 'here, when executing Task CL, use this cl.exe instead of the usual one'? msbuild's command line options don't contain anything obvious in that direction.
Also, is there a way to tell it to supply or override command line parameters for cl without changing the project file?
This is easy to do from either command line or project file. The properties you need to configure are $(CLToolExe) and $(CLToolPath).
From the command line:
msbuild MyProj.vcxproj /p:CLToolExe=clang-cl.exe /p:CLToolPath=c:\whatever\path\to\the\tool
Alternatively, inside your .vcxproj file:
<PropertyGroup>
<CLToolExe>clang-cl.exe</CLToolExe>
<CLToolPath>c:\whatever\path\to\the\tool</CLToolPath>
</PropertyGroup>
If you are calling task CL directly inside your .vcxproj file, as opposed to just relying on common targets, just set corresponding parameters ToolExe and ToolPath of the CL task.
Since Visual Studio 2019 16.2, Microsoft provide an integration of MSbuild and ClangCl. So this can be achieved by:
Installing the “C++ Clang Tools for Windows” component
Choosing the "LLVM (clang-cl)” toolset in the IDE
Microsoft's blog post has more information on this: https://devblogs.microsoft.com/cppblog/clang-llvm-support-for-msbuild-projects/
I were using #seva solution for sometime, though in Visual studio version Version 16.10.1 it works for me only if the 'CL' prefix is omitted from the command line parameters. i.e.:
msbuild MyProj.vcxproj /p:ToolExe=clang-cl.exe /p:ToolPath=c:\whatever\path\to\the\tool
I am using cmake to generate project files for a C++ project that needs to be compiled under Visual Studio 6 and 2010. The files are generated OK for both projects and I can build the projects without a problems.
However, the 2010 vxproj files contain relative paths to the cpp files and when I use Jenkins to build the files the log contains relative paths that Jenkins can not use to find the source files.
I see this:
..\..\source\moduleA\file1.cpp(74): warning C4800: 'BOOL' : forcing value to bool 'true' or 'false' (performance warning)
While it should have been either source/moduleA/file.cpp or D:\jenkins\jobs\workspace\source\moduleA\file.cpp for Jenkins to be able to find the file.
Of course, I can make a parser to parse the log file and remove the ..\...\ but I am hoping to find a more elegant solution.
In the end I found a compiler option that can fix this. You can add the /FC flag for Visual Studio 2010. Not sure if it works for VC6. To add it use this:
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /FC")