Using G++ to compile multiple *.cpp and *.h files in Sublime 2

99封情书 提交于 2019-12-01 08:22:49

问题


On a MAC I can successfully compile a c++ program from the command line using

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

However it fails from within Sublime 2 - I created a build system for this using

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

With these results

 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]

However, if I create the build system with specific filenames in the project it works:

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

How can I create a build system in Sublime 2 that uses command line patterns to compile multiple files like I can on the command line?


回答1:


Thanks hyde.

After playing with the build system based on your suggestion, this works:

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



回答2:


You should perhaps use something like this:

{
 "cmd" : ["gmake"]
}

Or possibly just make instead of gmake. But if you have gcc, GNU make should be in same directory. Below example Makefile is tested with GNU Make, it might not work without small modifications elsewhere.

So here's a very primitive Makefile for you. Important! It should be named Makefile so GNU Make will find it without arguments, and in it you must use actual tab char for indentation (before g++ and rm command below).

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

But even using hand-written Makefile can be considered primitive these days. You should perhaps install and learn to use CMake.



来源:https://stackoverflow.com/questions/21956428/using-g-to-compile-multiple-cpp-and-h-files-in-sublime-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!