如何在MacOS(Xcode)创建可在Windows(VS2017)打开构建的C++项目?
Cross-Platform C++ Project Workflow: Xcode (macOS) → Visual Studio 2017 (Windows)
Hey there! I’ve got a reliable, straightforward solution to let you develop C++ projects in Xcode on macOS and seamlessly build/run them in Visual Studio 2017 on Windows without recreating the project from scratch. The key here is using CMake—a cross-platform build system that generates IDE-specific project files (Xcode, VS, etc.) from a single configuration file.
Here’s how it works step by step:
1. Set Up Your Project with CMake
First, structure your project to be CMake-friendly. Create a root directory with this layout:
MyProject/ ├── src/ │ ├── main.cpp │ └── your_other_source_files.cpp └── CMakeLists.txt
Write a basic CMakeLists.txt file in the root directory—this is the core configuration that tells CMake how to generate your project files:
# Minimum CMake version required (adjust if needed) cmake_minimum_required(VERSION 3.10) # Name your project project(MyCrossPlatformApp) # Set the C++ standard (match what you need, e.g., 11, 17) set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) # Collect all your source files from the src folder file(GLOB_RECURSE SOURCES "src/*.cpp" "src/*.h") # Generate the executable add_executable(MyCrossPlatformApp ${SOURCES})
2. Develop and Build on macOS with Xcode
- Create a
buildfolder inside your project root (this keeps IDE-generated files separate from your source code):mkdir build && cd build - Run CMake to generate an Xcode project:
cmake .. -G Xcode - Open the generated
MyCrossPlatformApp.xcodeprojfile in Xcode. You can now write code, debug, and run your project as you normally would. All your source changes stay in thesrcfolder—no need to touch thebuilddirectory when moving to Windows.
3. Move to Windows and Build with Visual Studio 2017
- Copy the entire project root directory (including
src,CMakeLists.txt) to your Windows machine. - Open the Visual Studio 2017 Developer Command Prompt (this ensures CMake has access to VS's build tools).
- Navigate to your project root, create a
buildfolder, and run CMake to generate a VS 2017 solution:mkdir build cd build # For 32-bit projects cmake .. -G "Visual Studio 15 2017" # For 64-bit projects cmake .. -G "Visual Studio 15 2017 Win64" - Open the generated
MyCrossPlatformApp.slnfile in Visual Studio 2017. You’ll see all your source files, and you can build/run the project immediately—no need to recreate any project structure.
Key Tips for Smooth Cross-Platform Work
- Write cross-platform code: Avoid macOS-specific APIs (like Carbon or Objective-C calls) and stick to standard C++ or cross-platform libraries (e.g., STL, Boost).
- Use relative paths: In your code and CMake config, avoid hardcoded absolute paths. Use CMake variables like
${PROJECT_SOURCE_DIR}to reference files relative to your project root—CMake handles path separators (/vs\) automatically. - Manage third-party libraries: If you use external libraries, use CMake’s
find_package()to locate them, or include the library’s source code in your project so it builds alongside your app on both platforms.
内容的提问来源于stack exchange,提问作者user8773008
相关产品推荐
相关产品推荐

