2

在 MAC 上,我可以使用命令行成功编译 c++ 程序

  g++ *.cpp *.h -o executablename

但是它在 Sublime 2 中失败了——我为此创建了一个构建系统

 {
 "cmd" : ["g++", "*.cpp", "*.h", "-o", "executablename"]
 }

有了这些结果

 i686-apple-darwin11-llvm-g++-4.2: *.cpp: No such file or directory
 i686-apple-darwin11-llvm-g++-4.2: *.h: No such file or directory
 i686-apple-darwin11-llvm-g++-4.2: no input files
 [Finished in 0.0s with exit code 1]

但是,如果我在项目中创建具有特定文件名的构建系统,它可以工作:

{
"cmd" : ["g++", "Test.cpp", "TestCode.cpp", "TestCode.h", "TestCode2.cpp", "TestCode2.h", "-o", "executablename"]
}

如何在 Sublime 2 中创建一个构建系统,它使用命令行模式来编译多个文件,就像在命令行上一样?

4

2 回答 2

7

谢谢海德。

根据您的建议使用构建系统后,这有效:

{
"cmd" : ["g++ *.cpp -o executablename"],
"shell":true
}
于 2014-02-22T15:51:30.270 回答
0

你也许应该使用这样的东西:

{
 "cmd" : ["gmake"]
}

或者可能只是make代替gmake. 但是如果你有gcc,GNU make 应该在同一个目录中。下面的示例Makefile使用 GNU Make 进行了测试,如果不进行其他地方的小修改,它可能无法工作。

所以这里有一个非常原始的 Makefile 供你使用。重要的!它应该被命名Makefile以便 GNU Make 会在没有参数的情况下找到它,并且在其中您必须使用实际的制表符字符进行缩进(在下面的g++rm命令之前)。

CXXFLAGS := -Wall -Wextra $(CXXFLAGS) # example of setting compilation flags

# first rule is default rule, commonly called 'all'
# if there many executables, you could list them all
all: executablename

# we take advantage of predefined "magic" rule to create .o files from .cpp

# a rule for linking .o files to executable, using g++ to get C++ libs right 
executablename: TestCode.o TestCode2.o Test.o
    g++ $^ -o $@

# $^ means all dependencies (the .o files in above rule)
# $@ means the target (executablename in above rule)

# rule to delete generated files, - at start means error is ignored
clean:
    -rm executablename *.o

但如今,即使使用手写的 Makefile 也被认为是原始的。您或许应该安装并学习使用CMake

于 2014-02-22T16:46:58.233 回答