Create XCode workspace and pin a project from command line - xcode

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.

Related

Visual Studio CMake on cloned repo with many projects

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.

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.

How to install VTK 6.1 for OSX 10.8 with Cocoa/XCode support?

The extremely helpful guidelines posted at http://www.vtk.org/Wiki/Cocoa_VTK and via the readme file by Sean McBride and Mike Jackson inside the VTK repo were slightly out of date for VTK 6.1. So in case this helps anybody, I'm posting instructions for installing VTK 6.1 on OSX 10.8 with support for the SimpleCocoaVTK Xcode project.
* Installing VTK 6.1 for OSX 10.8 with Cocoa support *
These instructions slightly modify Ryan Glover's instructions at http://www.vtk.org/Wiki/Cocoa_VTK and the README.rtf in the VTK/Examples/GUI/Cocoa/Documentation folder by Sean McBride and Mike Jackson.
Clone the VTK git repo into a directory of your choice:
cd /Users/you/
git clone https://github.com/Kitware/VTK.git
cd VTK
git checkout tags/v6.1.0
make a build directory
mkdir VTKBuild
cd VTKBuild
Run the VTK cmake script
You will now be inside /Users/you/VTK/VTKBuild, run cmake from here (using the parent directory's CMake files):
cmake ..
Edit lots of lines in the newly generated CMakeCache.txt (in the current VTKBuild directory). One issue I had was that there were error if I didn't use a full path for the CMAKE_INSTALL_PREFIX. So make sure to use "/Users/you/" instead of "~":
CMAKE_INSTALL_PREFIX:PATH=/Users/you/VTK/VTKBuild
BUILD_SHARED_LIBS:BOOL=OFF
CMAKE_BUILD_TYPE:STRING=Debug
VTK_USE_SYSTEM_ZLIB:BOOL=ON
CMAKE_OSX_ARCHITECTURES:STRING=i386;x86_64
CMAKE_OSX_SYSROOT:STRING=/Applications/XCode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk
Compile the VTK project (this might take over an hour to run!):
make
Copy headers to an include directory:
make install
VTK should now be completely installed in the VTKBuild directory and ready to use in an XCode project!
Go to your finder, navigate to Users/you/VTK/Examples/GUI/Cocoa/ and double click to open SimpleCocoaVTK.xcodeproj in XCode.
In the XCode menubar (at the top of the screen) Go to Preferences->Locations->Source Trees and use the + button to add in two source trees:
vtk-debug-include vtk-debug-include Users/you/VTK/VTKBuild/include/vtk-6.1
vtk-debug-lib vtk-debug-lib Users/you/VTK/VTKBuild/lib
Click on the XCode project and delete all the references to vtk 6.0:
In the project view, select Targets->SimpleCocoaVTK and then press "Build Phases" and then open the "Link Binary With Libraries". Delete all the files that begin with "libvtk" and end with "6.0.a"
In the file view of the SimpleCocoaVTK project, hightlight and delete all the files in the vtk-libraries folder.
Make sure the XCode file view is active. Then in the finder, navigate to /Users/you/VTK/VTKBuild/lib, and select all the files that begin with "libvtk" and end with "6.1.a". Drag these files into the folder "vtk-libraries" in the XCode file view.
In XCode, do a Product->Clean
You can now build and run the sample SimpleCocoaVTK project.
I also had to set
VTK_WRAP_PYTHON:BOOL=ON
in CMakeCache.txt
It depends on what user you are too on your machine (computer) and the permissions relevant to that user. I did a find and replace on the CMakeCache.txt file and changed all /usr/local references to /Users/myusername/Develop/VTKInstall. That way everything's at your fingertips and you don't have to change permissions on things.
When you open up the Cocoa example make sure to set in you preferences these paths (e.g. Preferences->Locations->Source Trees). Also you'll need to re-import your vtk-libraries into the project.
I'm running Yosemite with XCode 6.1.1. I hope this helps someone!
If you get error messages likes this, when trying to build VTK:
#error: garbage collection is no longer supported
make[2]: *** Rendering/OpenGL/CMakeFiles/vtkRenderingOpenGL.dir/vtkCocoaRenderWindowInteractor.mm.o] Error 1
make1: *** [[Rendering/OpenGL/CMakeFiles/vtkRenderingOpenGL.dir/all] Error 2
You need to remove a flag in the source CMakeLists.txt:
#IF(APPLE)
SET(VTK_OBJCXX_FLAGS_DEFAULT "-fobjc-gc")
SET(VTK_REQUIRED_OBJCXX_FLAGS ${VTK_OBJCXX_FLAGS_DEFAULT} CACHE STRING "Extra flags for Objective-C++ compilation")
MARK_AS_ADVANCED(VTK_REQUIRED_OBJCXX_FLAGS)
ENDIF(APPLE)#
Either outcomment or delete it all together. Then run cmake again in an empty build directory. Check in the generated CMakeCache.txt in your build directory if it contains a key like VTK_REQUIRED_OBJCXX_FLAGS, it shouldn´t, try running cmake in an empty build directory again.
This 'bug' maybe fixed in future VTK versions.
Source: [Solved] Build Qt 5.2.1 + VTK 6.1.0 + CMake 2.8.12.2

Create multiple executables in XCode

There are various versions of this question but I havnt been able to find what Im looking for.
I have a bunch of cpp files in the OpenCV samples directory. I would like to create a project in XCode that can compile and build all the executables. I opened a new project, added all the sample source files, included the correct header files and the right link paths and then got stuck because I dont know how to make the project build and link executables for each relevant cpp file. Im guessing Im doing this completely wrong so any help would be appreciated.
In Xcode 4, click on the Project icon in the navigator which should bring up a Projects and Targets section in the main pane. There should be an "Add Target" button at the bottom. Just add a new target and then specify which source files you want in the "Compile Sources" section in the "Build Phases" tab.
Xcode 3 is should be similar. Add a new target and tell it which source files to use.
HTH
You don't need XCode to build samples, OpenCV on Mac OS X makes use of cmake to generate makefiles which, in turn, are being consumed by gcc.
Please read:
http://opencv.willowgarage.com/wiki/Mac_OS_X_OpenCV_Port
So, assuming you have cmake installed, in OpenCV source directory run:
sudo cmake -D BUILD_EXAMPLES=ON -G "Unix Makefiles" .
You have Makefile in your 'samples' directory now and can compile samples:
cd samples
sudo make
The sample binaries will be put in the 'bin' directory. VoilĂ !

CMake, Xcode and Unix Makefile

I was using classic Unix Makefile generator until I found a webpage explaining CMake could produce a nice xcode project file using "cmake -G Xcode" so I ran that command in my source directory and I get a nice xcode project. This is great but I'm facing some drawback and was wondering if there are solutions to these :
now I can't build out of tree using unix makefiles (I use to run cmake ../myproj in a build dir and then make command) this doesn't work anymore (I suppose temp files in the project dir are responsible)
some of my headers are generated by cmake (myproj.h.in became myproj.h and is include in some tests for example) but I can't find those files in the xcode project I get.
Thanks in advance for any good advice on how I can combine xcode project and unix makefile to be able to use both.
I found the answer by myself (I mean asking the good person) :
Use 2 separate build directories for each type of build files. This will ensure each build directory has the correct build files and generated files for its use.
In the add_executable() command, in addition to the source files also include the headers and generated headers for those to show up in Xcode.
You can use xcodebuild to still have a proper Xcode project and build it from the command line/shell script/Makefile, e.g.
xcodebuild -project MyProject.xcodeproj -target MyTarget -configuration Release

Resources