CMake and Visual Studio resource files - c++

I am converting a C++ project created using Visual Studio 2005 to CMake and have stumbled upon a bit of a problem with resource files that are included in the project.
The project includes a .rc file, a bunch of .ico files and a .rc2 file.
The regular .rc file works fine in the generated project and uses the resource compiler. The .ico and .rc2 files however are causing problems when they are just being included, because in the generated project Visual Studio attempts to compile them using the C/C++ compiler.
I assume that these files are included by the .rc file, so it would probably work to just not include them in the CMakeLists.txt file, but since it is obviously possible to list them in the project (they are visible in the original project) I would like to do so, so that the user of the generated project can see that these files are being used.
What is the correct way to handle these extra VS resource files in CMake?

Try to set_source_files_properties(your.ico your.rc2 PROPERTIES LANGUAGE RC).

By default it shouldn't do anything with those files. The source file property LANGUAGE should be empty and thus the action for the file should be checked by the file type. Which shouldn't be anything since it's not something it should compile.
Check your CMakeLists.txt that is doesn't contain a set_source_files_properties command that would mess with that property.
If you want to do something with the files, here are two ways to do things:
With add_custom_target you can add them and run custom commands for them when you build the project. Granted that the files have changed.
With configure_file you can easily copy them to a build directory if needed. With the COPYONLY flag.

Related

No .lib file generated after building tiny C++ static library project

I decided on adding a tiny extra project to my visual studio solution which includes only a single header file(for now) with a mutex which allows only 1 thread to output to the console at a time. Since this is a functionality which all of my projects in my solution will need so I thought it will be best to add a separate project for it and add references to it on the other projects.
So I created an empty project named Commons, then added an header file logger_mutex.h and wrote the following code inside it.
#pragma once
#ifdef _DEBUG
#include <mutex>
std::mutex loggerMutex;
#endif
I changed the project type in properties from Application(.exe) to Static Library (.lib). Then I added the include path to this file in the other project properties. Also I added this Commons project as a reference to all my other projects. But now when I try to build the solution it gave the error LINK1104 cannot open file ../path/to/my/output/directory/Commons.lib
I investigated on the output directory and there was no file in there named Commons.lib. I tried rebuilding the Commons project separately, and even though visual studio said it built successfully I did not see the Commons.lib file appear on the output directory.
I tried it even without the other projects, in a completely different solution. It still did not generate any .lib file in the output directory. I think this should be verifiable as well.
So what am I missing here, is there some kind of minimum requirement needed to have to get a .lib output file generated? Is my code too small to generate a .lib output? I am using Visual Studio 2022.
Add one empty .cpp file in your lib project and a lib will be generated.
But as far as I am concerned, it will be better to #include the logger_mutex.h to other project's pch.h instead of as a library.

How to compile/build/link - the XLNT library?

I downloaded the xlnt library for working with Excel - https://github.com/tfussell/xlnt. Can you please tell me what to do with it further? Honestly, I still can’t understand how among all the files there are in the downloaded archive, and there are 100 of them .hpp and .cpp files, what should I do with them?
1)As far as I understand, they need to be processed by the Cmake program and it seems that the output should appear .lib file, but I absolutely can’t understand how to do it or maybe something else
2)Or how can they be directly processed by Visual Studio 2017?
PS:When I launch Smack, I select the Source folder, the Cmake generates many incomprehensible files, but among them there is not one file with the .lib extension.
Help me please.
With CMake you can obtain the visual studio 2017 project files
and then compile the xlnt library.
Unzip the archive file
Within the xlnt folder, create a new directory and call it build
Launche CMake (GUI)
Browse for the source code folder
Browse for the build folder xlnt/build
With the Configure button select the tool (IDE/compiler) that you want to use
Pressing the Generate button, CMake will generate (inside the build folder)
the visual studio 2017 project files you need to compile the xlnt library
Building the solution, VS 2017 produces the xlnt.lib, xlnt.exp and xlnt.dll files.
The source path you see in the CMake screenshot is incorrect. You must specify the path that contains the "top level" CMakeLists.txt file.
The correct directory is: C:/Users/Zver/source/repos/XLNT Library/xlnt-master.
The CMakeLists.txt file in the above directory tells CMake all necessary subdirectories.
At this point you will see the static compilation option of the library appear.
If you use the MinGW/GCC compiler you must prevent the path from containing empty spaces.
Then you need to rename the directory from "XLNT Library" to "XLNTLibrary". At this point the path would become:
C:/Users/Zver/source/repos/XLNTLibrary/xlnt-master.

QT Visual Studio plugin fails automatic moc with non-standard header extensions

I am currently trying to port a very large project to QT. Many of the headers in this project use the file extension .hh . I would ideally like to avoid having to rename all of these files, but it appears to be causing some issues for the QT Visual Studio plugin. I am using Visual Studio 2012 and QT 5.5.
I have set up a trivial project to test this problem. If i define a class in a .h file that inherits from QObject and contains the Q_OBJECT macro the Custom Build instructions are automatically added to the files properties and moc'd files are automatically generated. If i rename the file to have a .hh extension no Custom Build instructions are generated and no moc'd files are generated.
I have attempted adding hh to the "Filter" property of the folder/filter containing my header files. This seems to result in the Custom Build instructions being applied to the headers, the correct files being generated by MOC, but not automatically included into the solution. This results the build failing with linker errors.
I would be surprised if this wasn't configurable some how. Has anyone had any success with custom file extensions before?

Source directories in Visual Studio 2010

I am using OF in my project and I want to use some add-ons but I have to add .cpp files to my project in order to compile them. I don't like it. Is there any option so I could specify a folder to scan for source files and compile every .cpp file it finds?
I thought it might be Source Directories in VC++ Directories section but it didn't work. Then I don't really get what it does.
If you want to compile sources using Visual Studio, you will have to add them to your project.
There is nothing wrong about adding external sources to your project in a nice filter.
You can also create a makefile to be used by Visual Studio which will list sources you need.
I'm not aware of an option that does what you ask for in VS. The Source directories configuration is used for locating source files that go along with libraries that you are using in your project. This way you can use the library in its binary format without the need to recompile it every time you rebuild your project, but you can also step into its code while debugging.

Copying C++ API header files to a common directory

I have a Visual Studio 2008 solution comprised of several projects. Only some header files in each project represents API to the library built from the project.
Is there a way in Visual Studio to copy the files to a common directory prior compilation?
(I want to do it in order prevent including unintentionally header file I'm not supposed to)
Thanks
Dima
Yes, on the project menu, select properties->configuration properties->build events->pre-build event. In the command line section you can enter a copy command with your source and destination paths. You may find the $solutiondir macro useful when enterting your paths.
You can run a script before compilation to do this (see pre-build step) but I don't think this is what you are asking.
Do you mean how can you have header files in another directory used by several projectS? Simply add that directory to the "Additional Include Directories" field in project settings-C++ ->General
>