2013-10-05 41 views
1

我只是學習SDL,並已完成了twinklebear tutorials。我開始的代碼分割成多個文件後,我的makefile停止工作,我不得不手動編譯。它說:計劃不會生成文件編譯,但工作沒有它

make: *** No rule to make target `main.o', needed by `ooptest'. Stop. 
當我使用像 .cxx.o:的規則,或者,如果我做單獨的規則對於所有我 .cxx的的

g++ main.o -L/usr/local/lib -lSDL2 -Wl,-rpath=/usr/local/lib -o ooptest 
main.o: In function `main': 
main.cxx:(.text+0x247): undefined reference to `init(SDL_Window**, SDL_Renderer**)' 
main.cxx:(.text+0x42e): undefined reference to `quit(SDL_Window**, SDL_Renderer**)' 
collect2: error: ld returned 1 exit status 
make: *** [ooptest] Error 1 

我知道這是因爲它是唯一連接主。 o而沒有其他文件,但我不知道爲什麼。

這裏有7個相關文件(只跳過這些,除非他們幫助解決問題,我不知道如何使他們擾流板,裏面的東西): 生成文件:

CXX   = g++ 
SRCS  = main.cxx init.cxx quit.cxx 
SDL_LIB  = -L/usr/local/lib -lSDL2 -Wl,-rpath=/usr/local/lib 
SDL_INCLUDE = -I/usr/local/include 
CXXFLAGS = -Wall -c -std=c++11 $(SDL_INCLUDE) 
LDFLAGS  = $(SDL_LIB) 
OBJS  = $(SRCS:.cxx=.o) 
EXE   = ooptest 
all: $(EXE) $(SRCS) 
$(EXE): $(OBJS) 
    $(CXX) $< $(LDFLAGS) -o [email protected] 
main.o: 
    $(CXX) $(CXXFLAGS) main.cxx -o [email protected] 
init.o: 
    $(CXX) $(CXXFLAGS) init.cxx -o [email protected] 
quit.o: 
    $(CXX) $(CXXFLAGS) quit.cxx -o [email protected] 
#.cxx.o: 
# $(CXX) $(CXXFLAGS) $< -o [email protected] 
clean: 
    rm *.o && rm $(EXE) 

main.h :

#ifndef _MAIN_ 
#define _MAIN_ 
#include <string> 
#include <SDL2/SDL.h> 
#include <SDL2/SDL_image.h> 
const int WIDTH=640; 
const int HEIGHT=480; 
void lgErr(std::string); 
SDL_Texture*loadTex(const std::string&,SDL_Renderer*); 
void renderTex(SDL_Texture*,SDL_Renderer*,int,int); 
int main(void); 
#endif 

main.cxx:

#include <iostream> 
#include <SDL2/SDL.h> 
#include <SDL2/SDL_image.h> 
#include "main.h" 
#include "init.h" 
#include "quit.h" 

using namespace std; 

void lgErr(string msg){ 
    cout<<msg<<" error: "<<SDL_GetError()<<endl; 
} 

SDL_Texture* loadTex(const string &file,SDL_Renderer *ren){ 
    SDL_Texture *tex=0; 
    SDL_Surface *img=SDL_LoadBMP(file.c_str()); 
    if(img){ 
     tex=SDL_CreateTextureFromSurface(ren,img); 
     SDL_FreeSurface(img); 
     if(tex==0)lgErr("CreateTextureFromSurface"); 
    }else 
     lgErr("LoadBMP"); 
    return tex; 
} 

void renderTex(SDL_Texture *tex,SDL_Renderer *ren,int x,int y){ 
    SDL_Rect dst; 
    dst.x=x;dst.y=y; 
    SDL_QueryTexture(tex,NULL,NULL,&dst.w,&dst.h); 
    SDL_RenderCopy(ren,tex,NULL,&dst); 
} 

int main(){ 
    SDL_Window *win; 
    SDL_Renderer *ren; 
    init(&win,&ren); 
    SDL_Texture *bg=loadTex("bg.bmp",ren); 
    SDL_Texture *fg=loadTex("fg.bmp",ren); 
    if(bg==0 || fg==0)return 4; 
    int bw,bh; 
    SDL_QueryTexture(bg,NULL,NULL,&bw,&bh); 
    SDL_RenderClear(ren); 
    for(int i=0;i<WIDTH;i+=bw) 
     for(int j=0;j<HEIGHT;j+=bh) 
      renderTex(bg,ren,i,j); 
    int fw,fh; 
    SDL_QueryTexture(fg,NULL,NULL,&fw,&fh); 
    int x=WIDTH/2-fw/2; 
    int y=HEIGHT/2-fh/2; 
    renderTex(fg,ren,x,y); 
    SDL_RenderPresent(ren); 
    SDL_Delay(2000); 
    SDL_DestroyTexture(bg); 
    SDL_DestroyTexture(fg); 
    quit(&win,&ren); 
} 

init.h裏:

#ifndef _INIT_ 
#define _INIT_ 
#include <SDL2/SDL.h> 
#include <SDL2/SDL_image.h> 
int init(SDL_Window**,SDL_Renderer**); 
#endif 

init.cxx:

#include <iostream> 
#include <SDL2/SDL.h> 
#include <SDL2/SDL_image.h> 
#include "main.h" 
#include "quit.h" 

using namespace std; 

int init(SDL_Window** win,SDL_Renderer** ren){ 
    if(SDL_Init(SDL_INIT_EVERYTHING)!=0){ 
     lgErr("SDL_Init"); 
     return 1; 
    } 
    *win=SDL_CreateWindow("Lesson 2",100,100,WIDTH,HEIGHT,SDL_WINDOW_SHOWN); 
    if(*win==0){ 
     lgErr("CreateWindow"); 
     return 2; 
    } 
    *ren=SDL_CreateRenderer(*win,-1,SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); 
    if(*ren==0){ 
     lgErr("CreateRenderer"); 
     return 3; 
    } 
    return 0; 
} 

quit.h:

#ifndef _QUIT_ 
#define _QUIT_ 
#include <SDL2/SDL.h> 
#include <SDL2/SDL_image.h> 
void quit(SDL_Window**,SDL_Renderer**); 
#endif 

quit.cxx:

#include <SDL2/SDL.h> 
#include <SDL2/SDL_image.h> 
#include "main.h" 
#include "init.h" 

void quit(SDL_Window** win,SDL_Renderer** ren){ 
    SDL_DestroyRenderer(*ren); 
    SDL_DestroyWindow(*win); 
    SDL_Quit(); 
} 

最後,我使用GNU Make 3.81g++ (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3,和SDL2與圖像庫。

有什麼不對的.cpp.o makefile的規則和$(EXE)的規則?另外,我是否正確使用#include?

回答

2

我認爲這個問題是在這個部分:

$(EXE): $(OBJS) 
    $(CXX) $< $(LDFLAGS) -o [email protected] 

Make's documentation$<是第一要義的名字,所以這只是試圖與main.o.鏈接相反,使用$^,這應該是所有先決條件。

+0

謝謝你,這確實解決它。我也改變.cxx.o:爲%的.o:%CXX – hacatu

相關問題