configuring OpenCV for all projects Visual studio 2010 - c++

I'm working with OpenCV on Visual Studio 2010, it works fine, but I'm wondring if it's possible to make a configuration that will be valid for all projects in windows .
instead of configuring every project.
thanks in advance

Yes it is. You need to use CMake. Make a folder that contains all the projects of OpenCV in different subfolders. You will need a cmakelists.txt file in the top folder that includes all the subfolders. On each subfolder you will need to have your source codes in src folder and your headers in a include folder. CMake will create a VS studio project for all your opencv different projects and will find authomatically the libs and link them.
Now, how to create the cmakelists.txt of each OpenCV project? Something like this:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT( project_name )
FIND_PACKAGE( OpenCV REQUIRED )
TARGET_LINK_LIBRARIES( ${PROJECT_NAME} {OpenCV_LIBS} )
Also check this links to set your cmake opencv projects. It is a pain the first time, but believe me that then is realy easy to create your projects, it is automatic.

To specify a per-user directory list
1.On the View menu, click Property Manager.
2.In Property Manager, click a configuration-and-platform node; for example, Debug | Win32.
The node expands and displays a user property sheet such as Microsoft.Cpp.%platform%user, where %platform% is a system-defined value such as Win32 or X64. The %platform% value and the platform for the project must be the same.
3.
Either double-click the user property sheet, or click the user property sheet and then click Properties in the shortcut menu.
The %user Property Sheet%Property Pages dialog box is displayed, and the VC++ Directories node is highlighted.
4.
Edit the directory lists, as described earlier in step 3 of "To specify a project directory list".

Related

How to use Visual Studio with CMake AND preserve file structure

I use CMake to create C++ projects. Then I would like to use Visual Studio as my IDE. But the issue I face is that I can't get files to be structured properly.
Here is the problem:
Let's assume I have the following file structure on the disk.
And here is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.20)
set(sourceDirectory src)
set(includeDirectory include)
set(targetName vulkan-tutorial)
set(targetVersion 1.0.0)
project(${targetName} VERSION ${targetVersion})
file(GLOB src
${sourceDirectory}/*.cpp
${sourceDirectory}/**/*.cpp
${sourceDirectory}/**/**/*.cpp
)
add_executable(${targetName} ${src})
target_include_directories(${targetName} PRIVATE ${includeDirectory})
Here is what this project looks like in Visual Studio with filters.
Or without using VS-filters.
I can create new files, but they're not going to land, where I want them to. The behavior I want to achieve is extremely simple: I want to see the root directory of my project and its "src" and "include" folders. I want to be able to right-click them and create new folders and files. Then I want Visual Studio to create them inside the selected folder. Like it would be in every normal file-explorer.
One solution I could imagine is to generate project files not inside a "build folder", but in the root directory of the project itself, which is obviously a terrible solution and leads to a pollution of the project structure.
Summarized - here is the result I want to end up with (now I can achieve it only by generating the project in the root). But all the build files should be located in the corresponding "build" folder in the root of the project.
I also would like to add that I tried to use different functions for CMake that kind of group my files together, but I just ended up with lots of filters that reflect my project structure on the disk. Of course, I still wasn't able to add new folders and files dynamically.
I appreciate any help. Thanks in advance.
Simply use the Open Folder option in visual studio.
This will open the directory and automatically configure the project using CMake. It will display the tree independently from the CMake configuration structure.
Microsoft has a page CMake projects in Visual Studio, that explains how to properly import CMake projects in visual studio

Let CMake set the "Exclude From Build" option for a single source file in Visual Studio

In Visual Studio there is an "Exclude From Build" option in the properties page of each source file, that can be set to exclude the file from build, but keep it visible in the source tree:
Is there a way to set that specific property with CMake?
I found a VS_DEPLOYMENT_CONTENT property and tried that but without success (it doesn't seem to do anything).
The reason for using that property is mainly to keep the file in the project to be able to open and edit it from within Visual Studio.
Thanks in advance!
Unfortunately, I also can't find the answer how to set "Exclude From Build" for some files with cmake in the same project. For now, I have added all such files to the target created by add_custom_target.
if(MSVC)
add_custom_target(myproj.additional SOURCES ${otherHeaders} ${otherSources})
endif()
This will create an extra project in the solution. The files in this project will not compile and I can still edit and search in them.
Another option is to set the HEADER_FILE_ONLY property for sources that should not be compile:
if(MSVC)
set_source_files_properties(${otherSources} PROPERTIES
HEADER_FILE_ONLY TRUE
)
endif()
In this case, the files stay in the project but without any marks - such files will not differ in appearance from those that are compiled
Since CMake 3.18 you can use VS_SETTINGS. That allows you to set any VS Project setting you like.
set_property(SOURCE ${SourceFiles} PROPERTY VS_SETTINGS "ExcludedFromBuild=true")
Note that you have to set the settings values with the name as it is written in the vcxproj file. To know what the setting is called you can set the setting to the correct value via the VS IDE, save the project and then open the vcxproj file in a text editor and search for the correct setting-value pair.
For example, in case of Excluded from Build:
<FXCompile Include="C:\path\to\source\file\file.hlsl">
<ExcludedFromBuild>true</ExcludedFromBuild>
</FXCompile>

How to hide targets in Visual Studio from CMake

I am generating a .sln with CMake.
I want to use Google Test and use that kind of code for adding a new tests:
add_executable(my_test test/my_test.cpp)
target_link_libraries(my_test gtest gmock_main)
add_test(NAME my_test COMMAND my_test)
It works fine, but when I open my .sln, I have all the targets appearing in the solution explorer: the libraries, the unit tests, etc.
Is there a way to hide these target?
You can't do it explicitly only in cmake (ATM), but here is one way on how can hide multiple targets more efficiently: Simply put them on in the same "folder" (in cmake) and then hide the folder (in visual studio).
Suppose you have cmake targets called Mm,Nn and Pp that you want to hide in Visual Studio. You need to say to cmake to allow "folders" and Simply set the property called FOLDER like so
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set_target_properties(Mm Nn Pp PROPERTIES FOLDER nameOfTheFolder)
and then right click on the folder nameOfTheFolderin solution and on hide folder.
If you want to see the hidden folders again, right click on the solution and then Unhide folders (this is at least how it is in visual studio 2010)

Is it possible to add files to a CMake generated solution folder in Visual Studio?

This question is more or less a warmup of this question:
how to get cmake to add files to msvcs solution tree
It never got a valid answer so I want to repose it slightly different:
Is it possible to use the cmake solution folders that where introduced with cmake 2.8.3 to add files directly to the vs solution? I want to do the cmake equivalent of VS->Solution->Add->Existing Item. So my file will appear in a folder that belongs to the solution and not to a project.
I found examples how the solution folders can be used to group targets into folders with code like this:
set_property( GLOBAL PROPERTY USE_FOLDERS ON)
set_property(TARGET ${TARGET_NAME} PROPERTY FOLDER "Test")
So can I add a file instead of a target to the folder?
This is probably not possible.
CMake organizes its assets into projects and targets. The project is what corresponds to Visual Studio's solution file while each target will generate a Visual Studio project inside the respective solution.
The problem is now that CMake does not allow to add files to CMake projects. In fact, a CMake project is little more than a collection of targets and offers almost no customization options. Hence the USE_FOLDERS option you mentioned can only be used to group VS projects inside a solution, but not single files.
The closest to what you ask would probably be to add a custom target that holds your files. However, this will still generate a VS project (which also contains a bunch of other stuff besides your files) inside the solution and not a plain folder.
If I understand the question correctly, I think I know how to do it:
set(FILES_TO_ADD
file1
file2
file3)
source_group("Group Name" FILES ${FILES_TO_ADD})
The source_group command creates the folder inside the Visual Studio project.
This answer just adds an example for the "custom target" solution that was proposed by ComicSansMS in the accepted answer. This is what I currently use to add my global files.
set( SOLUTION_FILES
${CMAKE_SOURCE_DIR}/CMakeLists.txt # root CMakeLists file
${CMAKE_SOURCE_DIR}/cmake/macros.cmake # cmake functions
${CMAKE_SOURCE_DIR}/CMakeInCodeDefinitions.h.in # imported cmake variables
# etc...
)
add_custom_target( GlobalFiles SOURCES ${SOLUTION_FILES})

Using Cmake with Qt Creator

I would like to use Qt creator and Cmake together (please, don't ask me about my motivation, accept this as a given.)
I successfully set up Qt creator to use cmake "Cmake": see this, this and this documents regarding how I did that.
I successfully create hello world project, but I can't create files in project,
only add existing files to project tree and after that adding it to cmake list.
Standard operation of Qt creator "Add New..." doesn't work and I can't find why.
Is there anybody who uses Qt creator and "Cmake" together? Is the combination actually possible?
Note: I'm using Qt creator v2.4.1.
You can add files using glob expression in your CMakeLists.txt, like this:
file(GLOB SRC . *.cpp)
add_executable (your_exe_name ${SRC})
Cmake will pick your new cpp files next time you run it and QtCreator will show them in the project browser.
Update
This solution may be useful but as noted in comments - this is not a good practice. Every time somebody add new source file and commit changes, you need to rerun cmake to build all the sources. Usually I just touch one of the CMakeLists.txt files if my build is broken after I pool recent changes from repository. After that make will run cmake automatically and I didn't need to run it by hands. Despite of that I think that explicit source lists in CMakeLists.txt is a good thing, they called thing CMake Lists for a reason.
When you add new files in QtCreator using the "New File or Project..." dialog it only creates the files on disk, it doesn't automatically add the files to the CMakeLists.txt. You need to do this by hand by editing the CMakeLists.txt file.
The next time you build the project, CMake will be re-run, and QtCreator will pick up the new files and show them in the project browser.
I solve this problem that I added new files in standard way (CTRL+N), then added needed files in CMakeLists. After that, right click on project in project tree view and choose option Run CMake. After this, files showed in project list tree. Only build was not enough.
I tested here and happened the same behavior because those options you were asking were really disabled.
Use File -> "New File or Project..." or CTRL+N to add new files and after that add to CMakeLists.txt
I'm adding an updated answer for newer versions of QtCreator (4.x, I don't know precisely which release but at least from 4.7). In the Tools > Options... menu, choose the Build & Run section and then the CMake tab. You will see the Adding Files settings, and you can set it to Copy file paths :
This way, when you want to add a new file to your project, in the Project view, right click on the desired CMake Executable/Library's name and select Add New..., go through the Add dialog, and when you'll validate the dialog, QtCreator will open CMakeLists.txt in the Editor view. Finally, paste the content of the clipboard at the end of the corresponding source file list and save CMakeLists.txt. The CMake project will be parsed, and your new file will show up in the Project view.