[英]Beginner Sublime & C++ Linker Setup
I am trying to use Sublime Text 2 as my IDE. 我正在尝试使用Sublime Text 2作为我的IDE。 I am setting up a project but having difficulty understanding how to properly link files. 我正在建立一个项目,但是很难理解如何正确链接文件。 When I build off of assn1.cpp, I get the following error: 当我从assn1.cpp构建时,出现以下错误:
C:\Users\..\AppData\Local\Temp\ccEffTCN.o:assn1.cpp:(.text+0xdd): undefined reference to `SceneData::SceneData(int)'
collect2.exe: error: ld returned 1 exit status
Here is my main cpp file. 这是我的主要cpp文件。 I remove the code that isn't causing the problem for the sake of clarity. 为了清楚起见,我删除了不会引起问题的代码。 I do not get a build error if I comment out where I instantiate a new Scene Data object. 如果注释掉实例化新场景数据对象的位置,则不会出现构建错误。
#include "SceneData.h"
using namespace std;
int main(int argc, char* argv[]){
SceneData sc = SceneData(1);
return 0;
}
For reference, here is my h and cpp file for SceneData. 供参考,这是我的SceneData的h和cpp文件。
// SceneData.cpp
#include "SceneData.h"
SceneData::SceneData(int x){
temp = x;
}
int SceneData::publicTemp(){
return 0;
}
#ifndef SCENEDATA_H
#define SCENEDATA_H
class SceneData{
int temp;
public:
SceneData(int x);
int publicTemp();
};
#endif // SCENEDATA_H
I am using the sublime provided c++ build config. 我正在使用提供的c ++构建配置的崇高精神。 mingw and g++ are installed. 已安装mingw和g ++。 I have all of the files saved in the correct project directory. 我将所有文件保存在正确的项目目录中。 My specific question is what am I missing to ensure files are linked when I create the o files that end up in the executable? 我的具体问题是,当我创建以可执行文件结尾的o文件时,我想确保文件被链接时会丢失什么?
** Edits Below * ** * *** **在下面进行编辑* ** * ***
This is my make file 这是我的make文件
OBJS = $(patsubst %.c, %.o, $(wildcard *.c)) \
$(patsubst %.cpp, %.o, $(wildcard *.cpp)) \
$(patsubst %.cxx, %.o, $(wildcard *.cxx))
CFLAGS = -Wall
CXXFLAGS = -Wall
OPT = -O4
balls2.png:
%.png: %.ppm
convert $< $@
%.ppm: %.nff trace
./trace < $*.nff
mv trace.ppm $@
trace: $(OBJS)
$(CXX) $(OPT) -o $@ $(OBJS) $(LDFLAGS) $(LDLIBS)
%.o: %.c
$(CC) $(OPT) -c -o $@ $(CFLAGS) $<
%.o: %.cxx
$(CXX) $(OPT) -c -o $@ $(CXXFLAGS) $<
%.o: %.cxx
$(CXX) $(OPT) -c -o $@ $(CXXFLAGS) $<
clean:
rm -f *.o
clobber: clean
rm -f trace *.ppm *.png
assn1.o: assn1.cpp SceneData.h
SceneData.o: SceneData.cpp SceneData.h
I had no problem compiling your code with g++, using Sublime as my text editor. 使用Sublime作为文本编辑器,使用g ++编译代码没有问题。 Are you using a make file when compiling? 编译时是否使用make文件? If so, please post your make file code. 如果是这样,请发布您的make文件代码。 Without a make file, I just needed to enter: 没有make文件,我只需要输入:
g++ *.cpp
And then ./a.out gave me the output '0' as expected. 然后./a.out给了我预期的输出'0'。
Perhaps the following thread may be of assistance to you: Can't build C++ program using Sublime Text 2 也许以下线程可能对您有帮助: 无法使用Sublime Text 2构建C ++程序
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.