Visual Studio CMake on cloned repo with many projects - visual-studio

I have a git repo with many many projects in it. I am trying to use MSVC with Clang-cl and CMake, Git.
I started out building a project directly in MSVC:
CMakePresets.json
CMakeLists.txt
asource.file.cpp
When I was happy with the working code, I moved these files into /repo/subfolder, and committed them.
Now I am cloning my repo as a new project: I chose to "clone repository" at MSVC project creation, and I want to avoid building the /repo root CMake project. Instead I want to build the /repo/subfolder CMake project. How can I achieve this?
Imagine I am on the Linux command line. Instead of:
cd myrepo
mkdir -p build && pushd build
cmake .. -G Ninja
I want to:
cd myrepo/subfolder
mkdir -p build && pushd build
cmake .. -G Ninja
The difference is that I want to make use of a CMake project in a subfolder instead of the root folder. I have been unable to build anything but the root project.
I've tried:
Right-click CMakeLists.txt and Configure CMake - it configures the top-level CMake project instead of the file I am clicking on.
Right-click CMakePresets.json and click all the "Add..." etc. there. It seems to edit the presets file itself.

I could not figure out how to have many CMake builds in one project cloned from a repo, but I figured out how to open any folder as its own CMake project:
File -> Open -> CMake...
Select the subfolder as your project. Then MSVC will trample your CMakePresets.json (truncating it to 0 bytes) which you can reset with Git -> Undo Changes. Once you have undone the changes you will want to reset the CMake cache and reconfigure in order to avoid failed CMake compilation tests.
And now it should work. Unfortunately it doesn't seem to be very easy to clone a repo and then work with $project inside it.

Related

openJPEG installation: $ make make: *** No targets specified and no makefile found. Stop

So.
Coming from
OpenJPEG installation
The build method maintained by OpenJPEG is CMake.
UNIX/LINUX - MacOS (terminal) - WINDOWS (cygwin, MinGW)
To build the library, type from source tree directory:`
mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make
https://github.com/uclouvain/openjpeg/blob/master/INSTALL.md
After properly getting Cmake to build, mingw-get to get msys-make (and also msys 1.0) to even have a proper response from MingW64, no matter what I do, I am stuck in the same error.
Downloaded first from the openjpeg-v2.3.1-windowsx64 link which the above proceeding seems to do nothing, as there is no CMakeList file there, then from the 'Source Code' openjpeg-2.3.1 which I can build from but that's it.
Can't make, can't install, can't use.
Considered 'source tree folder' the downloaded folder itself, the src folder inside of it and even a src folder of a IntelliJ project.
What am I missing?
PATH VARIABLE: %USERPROFILE%\AppData\Local\Microsoft\WindowsApps;
%IntelliJ IDEA Community Edition%;
C:\Users\Ajna\Desktop\jar2app\jar2app\jar2app_basefiles;
C:\Program Files\CMake\bin;
C:\Program Files\Git\mingw64\bin;
C:\MinGW\bin;
C:\Program Files\ffmpeg-4.2.1-win64-static\bin;
C:\msys\1.0\bin;
I've spent quite a while with this myself today. Here's what worked for me.
Download the whole code folder (green download code button) from
https://github.com/uclouvain/openjpeg
The downloaded folder is called "openjpeg-master". Save it in your downloads folder.
Install MinGW from https://www.ics.uci.edu/~pattis/common/handouts/mingweclipse/mingw.html
Install msys from
https://sourceforge.net/projects/mingw/files/MSYS/Base/msys-core/msys-1.0.11/MSYS-1.0.11.exe/download?use_mirror=altushost-swe
Open windows command line.
Navigate to the location of the "openjpeg-master" folder
e.g.
cd C:\#insert_location_of_your_downloads_folder#\Downloads\openjpeg-master
To specify the use of MinGW makefiles on windows use the following command
mkdir build
cd build
cmake -G "MinGW Makefiles" .. -DCMAKE_BUILD_TYPE=Release
make
The default for cmake on Windows is to use a Visual Studio generator: it generates project files for use with Visual Studio, not makefiles.
If you want it to generate makefiles you have to tell it specifically:
cmake -G 'Unix Makefiles' .. -DCMAKE_BUILD_TYPE=Release
You probably want to file an issue with the project and ask them to update their INSTALL file to have proper instructions.

Compile & Run CLion Project from Terminal

I am trying to create generally-accessible compile & run instructions for my CLion project, but can't find the exact terminal command it uses to execute the program (it's makefile, I would assume). From the project directory in the terminal, how would I do this?
The directory looks like this:
I will add a little bit to #Stanley F.'s excellent answer.
FROM the root of the CLion project, this is what works for me. I generally run with a debug profile. The same can be reproduced for release.
When cmake loads its project, it runs
cmake -Bcmake-debug-build -H. ${CMakeOptions}
where CMakeOptions is stored in CLion at
CLion->settings->Build,Execution, Deployment->CMake->[profile]->CMake Options
My general cmake build option is
-DCMAKE_BUILD_TYPE=debug -DSYTEM_ARCH=Linux-gcc5.3.0-x86_645 -CMAKE_CXX_STANDARD=14
[Note the lower-case d for 'debug'. If I do not use this, my system will not work. I wish that CLion did not default to 'Debug']
So, to reproduce what CLion creates upon project reload, I run
rm -rf cmake-debug-build
cmake -Bcmake-debug-build -H. -DCMAKE_BUILD_TYPE=debug -DSYTEM_ARCH=Linux-gcc5.3.0-x86_645 -CMAKE_CXX_STANDARD=14
Then, to build the project, I run
cmake --build cmake-build-debug --target all
Please note that when I run the first cmake command (from CLion or the command line), cmake pulls in lots of libraries from other "precedent" projects as part of the processing of my CMakeLists.txt file. If anything in one of those precedent projects changes, I will not pull them in anew, unless I physically delete the entire cmake-build-debug/ directory. None of CLion's reset tool menu items from my experience will delete that file.
If I am running these commands from the CLion menus, then I have to physically delete the cmake-build-debug/ directory as well (if I have a change in one of the external libraries that I want to pull in).
CLion currently only supports CMake projects. So you have to invoke the CMake executable with the appropriate parameters for your project.
At first, you can delete the cmake-build-debug folder, since this is auto-generated by CLion, which itself invokes CMake. It only contains temporary files.
So your build environment basically contains the 3DTable.c, 3DTable.h and CMakeLists.txt files. At least this is what I get from the screenshot.
To build the project from command line, first navigate to the source directory. Then invoke CMake:
cd <source path of Project_1>
cmake -Bbuild -H.
cmake --build build --target all
Notes:
build is the directory, where CMake will generate temporary files and the build artifacts.
The -H. option tells CMake, where the CMakeLists.txt file is located, which in this case is the current working directory.
The library / executable for your project will be located within the build directory
CLion can tell you, you don't need to hunt.
CMake command line
Select tools\cmake\reload cmake project.
The command line is shown in the CMake window.
Build command line
Select build\build project.
The command line is shown in the messages window.
Example
Mine look like this:
"C:\Program Files\JetBrains\CLion 2021.2.2\bin\cmake\win\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug "-DCMAKE_MAKE_PROGRAM=C:/Program Files/JetBrains/CLion 2021.2.2/bin/ninja/win/ninja.exe" -G Ninja -S C:\some_application -B C:\some_application\cmake-build-debug
...
"C:\Program Files\JetBrains\CLion 2021.2.2\bin\cmake\win\bin\cmake.exe" --build C:\some_application\cmake-build-debug --target all -j 9
Reminder
If using Visual Studio you still to specify which environment you are using. Typically this involves using the VS command prompt or executing one of the premade scripts to set up the environment variables. See here.

CLion build process does not start make install [duplicate]

I'm evaluating CLion 1.2.1 on an existing project which is already using CMake. The project is made up of a few library modules and a single executable.
I have an install target which I use to collect the executable and a configuration file together in a bin folder for debugging:
...
install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_BINARY_DIR}/bin/)
install(FILES config.xml DESTINATION ${CMAKE_BINARY_DIR}/bin/)
When building on the command line I'd just run:
make install
which as expected builds the binaries and if successful then runs the above install commands.
My problem is that I can't get CLion to run the 'install' target. I expected to be able to create a new Run/Debug configuration but the Target: dropdown only contains those targets added using add_executable() and add_library().
I also tried adding 'install' to the Build options in the Settings dialog. That however runs install for every target now including 'clean' which is not right.
UPDATE: As of 2018.1 EAP, build 181.3741.16, CLion supports running cmake install if your project defines install targets:
(source: cloudfront.net)
Original Answer:
I don't think that CLion implements this feature yet. However, you can work around this limitation by adding a CMake "custom target" (using add_custom_target()) that will execute the make install command:
add_custom_target(install_${PROJECT_NAME}
$(MAKE) install
DEPENDS ${PROJECT_NAME}
COMMENT "Installing ${PROJECT_NAME}")
Now, all you have to do is "build" the install_YOUR_PROJECT_NAME target from the "targets" menu in CLion.
Update:
A more cross-platform technique might be the following:
add_custom_target(install_${PROJECT_NAME}
"${CMAKE_COMMAND}" --build "${CMAKE_BINARY_DIR}" --target install
DEPENDS ${PROJECT_NAME}
COMMENT "Installing ${PROJECT_NAME}")
#maddouri 's comment already addresses your question. Alternatively, Under Settings -> Build, Execution, Deployment -> CMake, you can also set Build Option for Debug or Release build type to something like -j 2 install. With this setting, whenever CLion builds the code, it will install your targets, too!

Create XCode workspace and pin a project from command line

I found that it is possible to create XCode project from command line with a help of CMake (are there any other options?). Is there any tool to create XCode workspace and pin projects into it?
If not, can I create it manually? My current version of XCode (7.3 beta) makes a workspace that consists of below file structure:
WORKSPACE_NAME.xcworkspace/
contents.xcworkspacedata
xcuserdata/
USER_NAME.xcuserdatad/
UserInterfaceState.xcuserstate
Content of contents.xcworkspacedata looks straightforward and I guess this is the file that pins projects into workspace.
UserInterfaceState.xcuserstate is a binary file that can be generated when workspace is opened in XCode.
Summing up, if there is no command line tool for creating workspace and pinning projects into it, can I just generate proper contents.xcworkspacedata file or should I do something more?
If your project has a CMakeLists.txt file, CMake can generate an XCode project file from it (on the command line), and you can then build it as well (from the command line).
Assuming you are currently in your project's source directory and it contains a CMakeLists.txt:
$ mkdir build && cd build
$ cmake -G "Xcode" ../
$ cmake --build .
Alternatively you can remove the -G option, it'll then generate a Makefile, and then you can do make and it will build on the command-line too, using the Xcode build tools.

cmake generate Xcode project from existing sources

This is what I have, when I started generation:
iMac:IXCSoftswitch alex$ /usr/bin/cmake -G Xcode .
-- CMAKE_SOURCE_DIR = /Users/alex/Desktop/ixc-v/IXCSoftswitch, CMAKE_BINARY_DIR = /Users/alex/Desktop/ixc-v/IXCSoftswitch
CMake Error at CMakeLists.txt:25 (MESSAGE):
Binary and source directory cannot be the same
-- Configuring incomplete, errors occurred!
How can I fix it? I'm new to CMake, samples appreciated.
CMake is used to produce out-of-source builds.
The idea here is that you don't mix the files created during compilation with the original source files. In practice, you usually run CMake from a new, empty build directory and give the path to the source directory as an argument.
cd IXCSoftswitch
mkdir build
cd build
cmake -G Xcode ..
All of the files generated by CMake (which could be a lot) will now go into the build subdirectory, while your source directory stays clean of build artifacts.
The concept of out-of-source builds may seem strange at first, but it is actually a very convenient way of working once you get used to it.
From the root of your project directory.
cmake -G Xcode -H. -B_build
This is similar to the answer above. However, you are manually managing the out of source build. -B sets your target build directory (I happen to prefer _build). I tried looking up -H to double check, but couldn't find it. If memory serves, it specifies where your CMakeLists.txt lives.
I keep this command in a .sh/.bat file (depending). This way, I can keep my scripts that build my project in the root where a new person can easily find them.

Resources