2015-11-14 45 views
0

我有3個文件,main.cpp,Graphics.cpp和Graphics.h。函數定義在Graphics.h中。這些函數在Graphics.cpp中。主程序在main.cpp中。代碼編譯得很好,但不會構建。如果沒有包含函數列表,C++程序將不能編譯

當我運行

$ g++ -Wall -o "main" "main.cpp" -lSDL -lSDL_image -lSDL_mixer -lSDL_ttf    
/tmp/cce3Gqez.o: In function `main': 
main.cpp:(.text+0xfd): undefined reference to `Graphics::clear(int, int, int)' 
main.cpp:(.text+0x207): undefined reference to `Graphics::drawPixel(int, int, int, int, int)' 
main.cpp:(.text+0x364): undefined reference to `Graphics::drawRect(int, int, int, int, int, int, int)' 
main.cpp:(.text+0x4b7): undefined reference to `Graphics::fillRect(int, int, int, int, int, int, int)' 
main.cpp:(.text+0x4c5): undefined reference to `Graphics::flip()' 
/tmp/cce3Gqez.o: In function `InitProgram()': 
main.cpp:(.text+0x54f): undefined reference to `Graphics::init(int, int, bool)' 
collect2: error: ld returned 1 exit status 

的reffrences給函數的arent承認? 當我在main.cpp的頂部包含「#include」Graphics.cpp「」時,程序運行正常。我相信這是不適當的,但我無法以其他方式使用它。 這裏有3個源文件:

#include "Graphics.h" 
//#include "Graphics.cpp" 

const int FPS = 30; 
const int FRAME_TIME = 1000/FPS; 
const int SCREEN_WIDTH = 800; 
const int SCREEN_HEIGHT = 600; 
const int FULLSCREEN = false; 

Graphics graphics; 

bool InitProgram(); 
void FreeProgram(); 
bool ProgramRunning(); 

int main(int argc, char *argv[]) 
{ 
    if(!InitProgram()) 
    { 
     FreeProgram(); 
     return false; 
    } 

    int counter = 0; 

    while(ProgramRunning()) 
    { 
     int frameStart = SDL_GetTicks(); 

     counter++; 

     if(counter > 90) 
     { 
      counter = 0; 
      graphics.clear(rand()%255, rand()%255, rand()%255); 
     } 

     for(int i = 0; i < 100; i++) 
      graphics.drawPixel(rand()%SCREEN_WIDTH, rand()%SCREEN_HEIGHT, rand()%255, rand()%255, rand()%255); 

     graphics.drawRect(rand()%SCREEN_WIDTH, rand()%SCREEN_HEIGHT, rand()%100, rand()%100, rand()%255, rand()%255, rand()%255); 
     graphics.fillRect(rand()%SCREEN_WIDTH, rand()%SCREEN_HEIGHT, rand()%100, rand()%100, rand()%255, rand()%255, rand()%255); 

     graphics.flip(); 

     int frameTime = SDL_GetTicks()-frameStart; 
     int delay = FRAME_TIME - frameTime; 

     if(delay > 0) 
      SDL_Delay(delay); 
    } 

    FreeProgram(); 

    return 0; 
} 

bool InitProgram() 
{ 
    if(SDL_Init(SDL_INIT_EVERYTHING) == -1) 
     return false; 

    if(!graphics.init(SCREEN_WIDTH, SCREEN_HEIGHT, FULLSCREEN)) 
     return false; 

    SDL_WM_SetCaption("Graphics Test", NULL); 

    return true; 
} 

void FreeProgram() 
{ 
    SDL_Quit(); 
} 

bool ProgramRunning() 
{ 
    SDL_Event event; 

    while(SDL_PollEvent(&event)) 
    { 
     if(event.type == SDL_QUIT) 
      return false; 
    } 

    return true; 
} 

我能做些什麼,使這個編譯正常嗎?我是否缺少鏈接標誌?我正在使用g ++,而且我是新編寫的頭文件。什麼是編譯main.cpp的正確方法。

回答

1

您不應該使用預處理器到#include cpp源文件(*.cpp)。

您將必須鏈接Graphics.cpp或從它編譯的目標文件。

試試這個:

$ g++ -Wall -o "main" "main.cpp" "Graphics.cpp" -lSDL -lSDL_image -lSDL_mixer -lSDL_ttf 
+0

嘿,如果我有多個cpp文件? – j0h

+0

鏈接所有需要的源文件或目標文件。 – MikeCAT