Use CMake to Generate Xcode C++ Project
What’s CMake
CMake is used to control the software compilation process using simple platform and compiler independent configuration files, and generate native makefiles and workspaces that can be used in the compiler environment of your choice.
If you are a C++ programmer, CMake is what you can’t miss, especially when you write cross platform apps.
When Learning a Computer Graphics Course this year, teacher upload an project with Makefile which doesn’t work on my M1 Mac. I use Xcode for C++ IDE, and it’s frustrued set up file path, include path, library path. Then I learned CMake, which you write a CMakeLists.txt
to set up your project, like targets, libraries, and it can generate build file for varies developing environment from UNIX Makefile(Which is the default build system), Visual Studio, to Xcode. What’s more, this makes you code portable, as you can’t send you Xcode Project to a friend who is a Windows user.
Besides, some library won’t provide you a compile version of the library, instead, you can download source code, and compile on your own computer. For example:
1 | git clone https://github.com/oatpp/oatpp.git |
CMake Learning Material
an CMakeList.txt example
1 | cmake_minimum_required(VERSION 3.15) |
Build Project
1 | mkdir Step1_build |
Simplest CMakeLists.txt Example
1 | add_library(MathFunctions mysqrt.cxx) |
Generate Xcode Project
The default generator is UNIX Make, you can change the generator to Xcode Using at newly maked build_xcode directory,
1 | cmake ${CMakeLists.txt file path} -G Xcode |
Remember, after you add files to your Xcode project, it hasn’t been add to your CMakeLists.txt
, if you regenerate xcode project with cmake, problems gonna happen. You can fix it be edit your CMakeLists.txt
, emm, it truly tedious add your file in CMakeLists.txt
every time, but I haven’t found a better way yet.
Xcode C++ Error
Date: Sun Dec 18 11:22:30 CST 2022
After I Updated Xcode 14.2 Last Night, when I reopen my c++ Project, this problem happened.
Problem fixed after I regenerate my xcode project in directory build_xode
:
1 | rm -r * |
- Post title:Use CMake to Generate Xcode C++ Project
- Post author:Meqt
- Create time:2022-12-17 01:42:13
- Post link:https://meqtmac.github.io/2022/12/17/cmake_xcode/
- Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.