4

我曾经用来Xcode构建和运行C++程序。

我使用命令行在我的Xcode项目中编译相同的源代码。

编译单个.cpp文件是可以的。

编译更复杂的项目(多个文件)是不行的。

我试过了gcc,,,,。g++clangclang++

主要问题是未定义的符号。

你能展示如何通过命令行编译复杂的项目(多个文件)吗?

4

1 回答 1

4

From your description, it sounds like you're not using the -c (compile, but don't link) flag. Steps to build your project:

  1. Compile all of the source files:

    c++ -c file1.cpp
    c++ -c file2.cpp
    c++ -c file3.cpp
    
  2. Link your final executable:

    c++ file1.o file2.o file3.o
    

    You can use an optional -o flag to specify the output program name. It probably defaults to a.out.

It would be better to use some kind of build tool (like make, for example) to automate this process, or you'll find it to be very error prone.

于 2013-04-21T04:07:14.687 回答