简体   繁体   中英

Beginner Sublime & C++ Linker Setup

I am trying to use Sublime Text 2 as my 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:

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. 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.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. mingw and g++ are installed. 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?

** Edits Below * ** * ***

This is my make file

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. Are you using a make file when compiling? If so, please post your make file code. Without a make file, I just needed to enter:

g++ *.cpp

And then ./a.out gave me the output '0' as expected.

Perhaps the following thread may be of assistance to you: Can't build C++ program using Sublime Text 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM