CMake Copying Multiple DLLs Failing - visual-studio

So I have seen the other posts regarding moving external dll files to the current project's .exe output location for use at runtime but I seem to be running into an odd issue that I can't find information on.
I am using the following custom command to copy my libfreenect2 dlls into my output directory for my project:
add_custom_command(TARGET kinect_feeds POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"libfreenect2_output_path/bin/*.dll"
$<TARGET_FILE_DIR:kinect_feeds>)
CMake sets up my project just fine, but when I go to run the command in Visual Studio it errors out when trying to copy the files. I think the issue is with the wildcard character. I used the error output in the Visual Studio to copy the complete command into by git bash window and it works as expected. Also Visual Studio has no problem moving multiple files if they are explicitly defined like so:
add_custom_command(TARGET kinect_feeds POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"libfreenect2_output_path/bin/freenect2.dll"
"libfreenect2_output_path/bin/glfw3.dll"
$<TARGET_FILE_DIR:kinect_feeds>)
My question is, do wildcard characters not work in CMake commands when being executed by Visual Studio or is there something that I am missing? For now I will just type out all of the DLL files explicitly, but I was hoping to avoid this.
I am using the latest version of CMake and Visual Studio 2015 Community Edition.

I'm running into the same issue with CMake 3.6.1 and Visual Studio 2012. I don't think Visual Studio has any impact though, because I get the errors from the command line as well
From a CMD prompt:
> cmake -E copy .\data\*.bin \temp
Error copying file ".\data\*.bin" to "\temp".
This question references a CMake bug report regarding wildcards, that was supposed to be fixed in CMake 3.5, but doesn't appear to work on Windows with CMake 3.6.1.
I think your solution to list each file individually is the current solution.

Related

CMake run shell command after building error

I have a Visual Studio 2019 CMake project and i need to run a postbuild script which copys a file (The file is not generated by the build process). What i did so far is add a custom command in CMake with
add_custom_command(TARGET testExec POST_BUILD COMMAND "../postbuild.bat")
The postbuild.bat would copy the file. This works great most of the time but when my build fails due to some compile error, the postbuild script won't be executed.
How can i run the postbuild script even if the build fails ? I know there is a similar question here but sadly no proper solution. If there is a way to configure a postbuild event directly inside a Visual Studio CMake project this would also be suitable, but it seems like this is not possible (because in a cmake project i don't have a project file).
Since The copied file is not generated by the build you can use PRE_BUILD. On Visual Studio Generators, it runs before any other rules are executed within the target.(https://cmake.org/cmake/help/latest/command/add_custom_command.html).
The other solution could be to use add_custom_command(OUTPUT as the file seems independent of the build.

Visual Studio 2017 - How to create a project from the source using CMake?

I have a pretty large software library using CMake to be compiled. We use Linux mostly, but now a new colleague wants to use Visual Studio.
Is there any way to create a new VS 2017 project from the existing source codes with CMake structure?
I know, it's possible to do it with CLion, but I have no idea about VS, as I have a very little experience with it.
Other questions seem to focus on creating an empty project, which will use CMake, but not on creating a project from already existing source files.
Creating a cmake project with visual studio
Creating a project with visual studio 2017
I'm not sure why you asked for details but...
Assuming you are using cmake 3.13 then you can do the following in a command shell:
cmake -G "Visual Studio 15 2017" -S path_to_source -B path_to_build
This will then create a solution file. Actually it creates a solution file for every project() command that is issued in CMakeLists.txt.
You can then open the solution file in Visual Studio, and build the project as usual.
You don't even need to do this in the Visual Studio GUI. After creating the initial project you can also issue the command:
cmake --build path_to_build
Which will kick off the build at the command line.
Now if your CMakeLists.txt in path_to_source is using Linux specific libraries or gcc specific compiler settings then the CMakeLists.txt will have to get updated to the Windows equivalent.
The alternative is to start Visual Studio and then use File->Open->CMake and open the CMakeLists.txt file in path_to_source. It'll then start to generate the project. But I prefer using the command line method.

Generate Visual Studio Project File to build a library with qmake syntax and where to find the project file?

I have an old script to create Visual Studio Project Files to build libraries. The syntax is
cd Kernel
cd core
qmake -t vclib
cd ccMigration
qmake -t vclib
cd ..
and so on for all of the different directories.
I understand why we use qmake and vclib, but what I am not sure is what is the use of -t in this line:
qmake -t vclib
Looking at this : http://doc.qt.io/archives/qt-5.5/qmake-platform-notes.html it says:
Creating Visual Studio Project Files This section describes how to
import an existing qmake project into Visual Studio. qmake is able to
take a project file and create a Visual Studio project that contains
all the necessary information required by the development environment.
This is achieved by setting the qmake project template to either vcapp
(for application projects) or vclib (for library projects).
This can also be set using a command line option, for example:
qmake -tp vc
Why do they use -tp and is the old script syntax correct by using -t?
After I run the script it takes about 1/2 seconds and where can I find the project files for Visual Studio?
Finally, When I open the project files how can I build a library using Visual Studio? Can someone point me to a place where I can read some material?
Thank you for all the help!
Edit: I ran the script and it created .vcproj files in the directories that qmake was run. Example: In folder Kernel -> core it created ccCore.vcproj project file. When I open the file in Visual Studio 2017 it wanted me to perform a One-way upgrade that will automatically make functional changes to the project in order to open it. Is this okay?

pdcurses creating the library for visual studio 2010 fails (cannot access)

I'm trying to create the library file for pdcurses but unfortunately I have absolutely no idea where to begin. If you could provide some steps for this it would be fantastic.
I'm using windows 8.1 if that changes anything
update
I got it working but now it is failing on cannot access complier made file insch.obj
Launch a Visual Studio 2010 command prompt. This sets your
environment properly, so that nmake.exe is in your PATH on Windows.
Next, "cd" into the "win32" subfolder that was created when you extracted the compressed tar distribution of PDCurses3.4.
Now, you are ready to build PDCurses using Visual Studio's compiler using this command:
To perform a "clean" build target:
nmake -f vcwin32.mak DEBUG=Y DLL=Y WIDE=Y UTF8=Y clean
To build PDCurses in "DEBUG" mode (so that you can step through code in Visual Studio) with "WIDE" and "UTF8" enabled:
nmake -f vcwin32.mak DEBUG=Y DLL=Y WIDE=Y UTF8=Y all

cmake and make in Windows

I understand that in linux cmake, make and make install can be combined together to produce a release. For example:
cmake -DCMAKE_BUILD_TYPE=Release ..
make
make install
In windows, however, I cannot find similar commands that can do the same job. Usually, what is done is to build a .sln project first if Visual Studio is used, after that compile the .sln project and in the end run the INSTALL project. Will it be possible to make a release with several commands as it has been done in Linux. Many thanks.
You can use msbuild instead of make:
cmake -G"Visual Studio 12" ..
msbuild /P:Configuration=Release INSTALL.vcxproj
or you could use CMake's --build argument:
cmake -G"Visual Studio 12" ..
cmake --build . --target INSTALL --config Release
If you need the equivalent of the make command with no args (i.e. make all) you would build the ALL_BUILD target as well, but this is built as part of the INSTALL target anyway.
In addition to Fraser's answer, to get the first solution to work, one might need to call MSBuild in CMD as Administrator from the original path
cmake ..
"C:\Program Files (x86)\Microsoft Visual Studio\YEAR\EDITION\MSBuild\15.0\Bin\MSBuild.exe" INSTALL.vcxproj -p:Configuration=Release
Replace YEAR and EDITION with your corresponding values e.g. 2017 and Community
You might receive the following error, which means your enviroment variable is not set.
error MSB4019: The imported project "C:\Microsoft.Cpp.Default.props" was not found.
Confirm that the path in the declaration is correct, and that the file exists
on disk.
The enviroment variable can be set directly in the system settings or with CMD as Administrator. (Source for reference)
for Visual Studio 2015 and below:
SET VCTargetsPath="C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\vXXX\"
Replace vXXX with your corresponding version e.g. 140
for Visual Studio 2017 and above:
SET VCTargetsPath="C:\Program Files (x86)\Microsoft Visual Studio\YEAR\EDITION\Common7\IDE\VC\VCTargets\"
Replace YEAR and EDITION with your corresponding values e.g. 2017 and Community
Edit:
I've also noticed that some solutions in Visual Studio 2017 wont show their contents in the Solution Explorer if the enviroment variable is set. Just remove them again if necessary.

Resources