2012-05-06 2 views
2

C++에서 opengl을 사용하여 간단한 obj 로더를 만들려고합니다. 하나의 명령으로 모든 것이 잘 작동 컴파일 ,g ++로 객체를 연결할 때 문제가 발생했습니다.

g++ -o main main.cpp timer.cpp screen.cpp obj_loader.cpp `sdl-config --cflags --libs` `pkg-config --cflags --libs glu` 

반환 오류없이.

컴파일 오브젝트는 개별적으로

g++ main.cpp -o main.o -c -Wall `sdl-config --cflags --libs` `pkg-config glu --cflags --libs` 
g++ obj_loader.cpp -o obj_loader.o -c -Wall `sdl-config --cflags --libs` `pkg-config glu --cflags --libs` 
g++ timer.cpp -o timer.o -c -Wall `sdl-config --cflags --libs` `pkg-config glu --cflags --libs` 
g++ screen.cpp -o screen.o -c -Wall `sdl-config --cflags --libs` `pkg-config glu --cflags --libs` 

는 오류를 반환하지, 잘 너무 작동합니다.
main.o: In function `draw()': 
main.cpp:(.text+0x1d): undefined reference to `glColor3f' 
main.cpp:(.text+0x27): undefined reference to `glBegin' 
main.cpp:(.text+0x36): undefined reference to `glVertex2i' 
main.cpp:(.text+0x45): undefined reference to `glVertex2i' 
main.cpp:(.text+0x54): undefined reference to `glVertex2i' 
main.cpp:(.text+0x63): undefined reference to `glVertex2i' 
main.cpp:(.text+0x68): undefined reference to `glEnd' 
main.o: In function `main': 
main.cpp:(.text+0xf8): undefined reference to `SDL_PollEvent' 
main.cpp:(.text+0x10b): undefined reference to `glClear' 
main.cpp:(.text+0x115): undefined reference to `SDL_GL_SwapBuffers' 
main.cpp:(.text+0x11a): undefined reference to `glFinish' 
main.cpp:(.text+0x14e): undefined reference to `SDL_Delay' 
timer.o: In function `Timer::start()': 
timer.cpp:(.text+0x4d): undefined reference to `SDL_GetTicks' 
timer.o: In function `Timer::pause()': 
timer.cpp:(.text+0xa6): undefined reference to `SDL_GetTicks' 
timer.o: In function `Timer::unpause()': 
timer.cpp:(.text+0xe5): undefined reference to `SDL_GetTicks' 
timer.o: In function `Timer::tick()': 
timer.cpp:(.text+0x136): undefined reference to `SDL_GetTicks' 
timer.o: In function `Timer::get_ticks()': 
timer.cpp:(.text+0x172): undefined reference to `SDL_GetTicks' 
screen.o: In function `init()': 
screen.cpp:(.text+0xa): undefined reference to `SDL_Init' 
screen.cpp:(.text+0x31): undefined reference to `SDL_SetVideoMode' 
screen.cpp:(.text+0x64): undefined reference to `SDL_WM_SetCaption' 
screen.o: In function `init_GL()': 
screen.cpp:(.text+0x80): undefined reference to `glClearColor' 
screen.cpp:(.text+0x8a): undefined reference to `glMatrixMode' 
screen.cpp:(.text+0x8f): undefined reference to `glLoadIdentity' 
screen.cpp:(.text+0xc0): undefined reference to `glOrtho' 
screen.cpp:(.text+0xca): undefined reference to `glMatrixMode' 
screen.cpp:(.text+0xcf): undefined reference to `glLoadIdentity' 
screen.cpp:(.text+0xd4): undefined reference to `glGetError' 
screen.o: In function `clean_up()': 
screen.cpp:(.text+0xf4): undefined reference to `SDL_Quit' 
collect2: ld returned 1 exit status 

내 포함 된 라이브러리

은 다음과 같습니다 : 최종

g++ main.o obj_loader.o timer.o screen.o -o main 

을 실행하는 경우에는

, 나는 정의되지 않은 참조 오류의 무리를 얻을 수

#include <iostream> 
#include <fstream> 
#include <string> 
#include <vector> 

#include "SDL/SDL.h" 
#include "SDL/SDL_opengl.h" 

#include "GL/gl.h" 
#include "GL/glu.h" 

내 메이크 :

CC=g++ 
SDL_FLAGS=`sdl-config --cflags --libs` 
GL_FLAGS=`pkg-config glu --cflags --libs` 

CFLAGS=-c -Wall 

FLAGS=$(CFLAGS) $(SDL_FLAGS) $(GL_FLAGS) 
LDFLAGS= 

SOURCES=main.cpp obj_loader.cpp timer.cpp screen.cpp 
OBJECTS=$(SOURCES:.cpp=.o) 
EXECUTABLE=main 

all: $(SOURCES) $(EXECUTABLE) 

$(EXECUTABLE): $(OBJECTS) 
    $(CC) $(OBJECTS) -o [email protected] $(LDFLAGS) 

.cpp.o: 
    $(CC) $< -o [email protected] $(FLAGS) 

clean: 
    rm -f *o main 
+0

예를 들어 명령 줄에서 작동하지 않는 명령 줄은 작동하는 라이브러리가 필요한 라이브러리를 링크하는 것을 보여주지 않습니다. 당신의 Makefile은 똑같은 문제를 가진 것 같습니다. 나는 그것이 작동하기를 기대하지 않을 것이다. –

답변

2

그래 ... 그 모든 것들이 --libs에 의해 반환됩니까? 최종 실행 파일을 올바르게 링크하려면이 파일들이 필요합니다.

0

OpenGL과 연결되도록 g ++에 알리는 데는 -llibraryname이 표시되지 않습니다. 나는 main (즉, 'main'으로 끝나는 라인)을 만드는 g ++ 라인에서 이것을 보게 될 것이다. 내가 아는 한, .o 파일 자체에는 참조를 해결하기 위해 라이브러리를 찾을 위치에 대한 정보가 들어 있지 않습니다. 이 정보는 명령 행에서 -l 스위치로 전달되어야합니다.

1

sdl-configpkg-config--cflags--libs의 두 플래그를 취합니다. 이러한 플래그 중 첫 번째는 소스 파일을 오브젝트 파일로 컴파일 할 때 필요합니다. 링크 할 때 두 번째 플래그가 필요합니다. 단일 명령으로 모든 것을 컴파일하면 두 단계가 단일 명령으로 처리되므로 모두 --cflags--libs에서 sdl-configpkg-config을 동시에 전달해야합니다. 이 단계를 분리 했으므로 적절한시기에 sdl-configpkg-config에 올바른 옵션을 전달해야합니다. 당신의 메이크에서

g++ main.cpp -o main.o -c -Wall `sdl-config --cflags` `pkg-config glu --cflags` 
g++ obj_loader.cpp -o obj_loader.o -c -Wall `sdl-config --cflags` `pkg-config glu --cflags` 
g++ timer.cpp -o timer.o -c -Wall `sdl-config --cflags` `pkg-config glu --cflags` 
g++ screen.cpp -o screen.o -c -Wall `sdl-config --cflags` `pkg-config glu --cflags` 

g++ main.o obj_loader.o timer.o screen.o -o main `sdl-config --libs` `pkg-config glu --libs` 

이 다음과 같이 변수를 구성하는 의미 : (나는 또한 당신의 메이크 파일에 몇 가지 다른 사소한 수정했습니다)

CC=g++ 
LD=g++ 

CFLAGS=-c -Wall $(shell sdl-config --cflags) $(shell pkg-config glu --cflags) 
LDFLAGS=$(shell sdl-config --libs) $(shell pkg-config glu --libs) 

SOURCES=main.cpp obj_loader.cpp timer.cpp screen.cpp 
OBJECTS=$(SOURCES:.cpp=.o) 
EXECUTABLE=main 

all: $(EXECUTABLE) 

$(EXECUTABLE): $(OBJECTS) 
    $(LD) $(LDFLAGS) $(OBJECTS) -o [email protected] 

.cpp.o: 
    $(CC) $(CFLAGS) $< -o [email protected] 

clean: 
    rm -f *.o main 

이 메이크에 대한 비판이 더 많이있다 - - GNU Make가 implicits로 내장되어 있기 때문에 아마도 여기에서 대부분의 액션 라인을 제거 할 수 있습니다. .o 파일의 종속성을 지정하는 규칙을 추가해야 할 수도 있습니다. .cpp 개의 파일에는 각각 .h 개의 파일이 포함되어 있으므로 .h 파일이 변경 될 때 .cpp 파일을 다시 컴파일해야합니다.

+0

그리고 makefile은'CFLAGS : = $ (shell sdl-config --cflags) 등 '을 사용하여 컴파일 된 .o에 대한 백틱 쉘 명령을 다시 실행하지 않도록 할 수 있습니다. –

+0

@JonathanWakely : 완료되었습니다. –

관련 문제