2011-11-25 2 views
6

자, 약 10 자습서를 읽지 만 항상 오류가 계속 발생합니다. 5 파일, main.cpp class.cpp, class.h 및 functions.cpp 및 functions.h가 있습니다. 이 모든 것들은 서로 다른 객체의 함수를 사용합니다. 즉, functions.cpp의 함수는 classes.cpp의 객체를 사용합니다. 나는 그것이 참조를 정의되지 않은 것을 말해 계속메이크 파일에서 정의되지 않은 참조

CC = g++ -O2 -I./sdl/include -L. 
LIBS = -lm -lSDL -lpthread -ldl 
SRC = main.cpp 
SDLF = SDLfunctions.cpp 
CLASS = classes.cpp 
CLASSH = classes.h 
SDLFH = SDLfunctions.h 

all: main 

main: SDLfunctions.o Classes.o $(SRC) 
    $(CC) -o [email protected] $(SRC) $(LIBS) 

SDLfunctions.o: $(SDLFH) $(SDLF) $(CLASS) $(CLASSH) 
    $(CC) -o [email protected] $(SDLF) $(LIBS) 

Classes.o: $(CLASS) $(CLASSH) $(SDLF) $(SDLFH) 
    $(CC) -o [email protected] $(CLASS) $(LIBS) 

을 다음과 같이

내 메이크 보인다. 나는 무엇을 놓치고 있습니까? 내가 Visual C++에서 그들을했을 때

무엇 메이크 출력

/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/../../../crt1.o: In function `_start': 
(.text+0x18): undefined reference to `main' 
/tmp/ccJG6yQA.o: In function `DrawEnemies(SDL_Surface*)': 
SDLfunctions.cpp:(.text+0x3a7): undefined reference to `Enemy::sprite' 
/tmp/ccJG6yQA.o: In function `rysujpociski(int, SDL_Surface*, SDL_Surface*, 
std::vector<AllyBullet, std::allocator<AllyBullet> >&, double)': 
SDLfunctions.cpp:(.text+0x141f): undefined reference to `AllyBullet::sprite' 
/tmp/ccJG6yQA.o: In function `global constructors keyed to width': 
SDLfunctions.cpp:(.text+0x14a7): undefined reference to `Enemy::Enemy()' 
collect2: ld returned 1 exit status 
make: *** [SDLfunctions.o] Error 1 

파일이 큰 컴파일, 그래서 내 메이크이어야한다.

+0

make 출력을 게시하십시오. 정의되지 않은 참조는 무엇입니까? – EFraim

+1

make를 실행하면 실행 된 명령이 표시됩니다. 수동으로 컴파일하기 위해 입력하는 명령과 다른 명령입니까? – rodrigo

+1

질문 제목을 개선하여 질문을 설명하십시오. –

답변

7

같은 첫 번째 옵션합니다. 오브젝트 파일을 컴파일 (-c) 한 다음 함께 링크해야합니다. 이것은 다음과 같을 것입니다 :

CC = g++ -O2 -I./sdl/include -L. 
LIBS = -lm -lSDL -lpthread -ldl 
SRC = main.cpp 
SDLF = SDLfunctions.cpp 
CLASS = classes.cpp 
CLASSH = classes.h 
SDLFH = SDLfunctions.h 

all: main 

main: SDLfunctions.o Classes.o $(SRC) 
    $(CC) -o [email protected] $(SRC) SDLfunctions.o Classes.o $(LIBS) # you forgot to link 
                 # the object files 

SDLfunctions.o: $(SDLFH) $(SDLF) $(CLASS) $(CLASSH) 
    $(CC) -o [email protected] -c $(SDLF)  # -c added to compile, not link 

Classes.o: $(CLASS) $(CLASSH) $(SDLF) $(SDLFH) 
    $(CC) -o [email protected] -c $(CLASS)  # -c added to compile, not link 

이 작업을 수행하는 동안 컴파일러 main.o를 별도로 설치하는 것이 좋습니다. 따라서 :

CC = g++ -O2 -I./sdl/include -L. 
LIBS = -lm -lSDL -lpthread -ldl 
MAIN = main.cpp 
SDLF = SDLfunctions.cpp 
CLASS = classes.cpp 
CLASSH = classes.h 
SDLFH = SDLfunctions.h 

all: main 

main: SDLfunctions.o Classes.o main.o 
    $(CC) -o [email protected] SDLfunctions.o Classes.o main.o $(LIBS) 

main.o: $(SDLFH) $(MAIN) $(CLASSH) 
    $(CC) -o [email protected] -c $(MAIN) 

SDLfunctions.o: $(SDLFH) $(SDLF) $(CLASS) $(CLASSH) 
    $(CC) -o [email protected] -c $(SDLF) 

Classes.o: $(CLASS) $(CLASSH) $(SDLF) $(SDLFH) 
    $(CC) -o [email protected] -c $(CLASS) 

또한 연결 후 발생하지 않기 때문에 -c를 사용할 때 나는 $(LIBS)을 제거 있습니다.

+0

고맙습니다. :) –

2

오타가 있습니다. $(CLASSESH)을 사용하고 있지만 CLASSH으로 신고했습니다.

+0

수정되었지만 도움이되지 않았습니다. –

6

.o 파일을 실행 파일에 연결하려고합니다. 컴파일 플래그에 -c를 추가하여 오브젝트 파일 만 컴파일합니다.

는 당신은 참으로 이상한 일을하고있는이

SDLfunctions.o: $(SDLFH) $(SDLF) $(CLASS) $(CLASSH) 
    $(CC) -c -o [email protected] $(SDLF) $(LIBS) 
+0

"g ++ : SDLfunctions.o : 파일이나 디렉토리가 없습니다" –