2014-06-11 64 views
0

我是C++編程的初學者(曾在PHP上工作多年),並且在獲得Visual Studio Express 2013編譯一些在Xcode和Eclipse中都能正常工作的代碼時遇到了一些麻煩。僅在Visual Studio中有未解析的外部符號?

所以設置是這樣的。我打算使用SDL 2.0爲手機和平板電腦創建遊戲。我正在使用Mac(使用VM for Windows),並且迄今已成功獲得了在Xcode和Eclipse中工作的初始測試項目。這兩個項目鏈接到使用Xcode創建的公共庫(用於跨平臺開發,所以我爲所有平臺編碼一次)。我現在正在嘗試添加適當的Visual Studio項目,以便我也可以爲Windows 8/WP8編譯它。

按照說明here我已成功設置了該頁面的基本測試工作,證明SDL在Windows 8中正常工作。但是,我現在正努力將我的通用庫鏈接到Visual Studio中的項目一直給我錯誤。首先是代碼。

的main.cpp

#include "SDL.h" 
#include "rectangles.h" 
#include <time.h> 

#define SCREEN_WIDTH 320 
#define SCREEN_HEIGHT 480 

int main(int argc, char *argv[]) 
{ 

    SDL_Window *window; 
    SDL_Renderer *renderer; 
    int done; 
    SDL_Event event; 

    /* initialize SDL */ 
    if (SDL_Init(SDL_INIT_VIDEO) < 0) { 
     printf("Could not initialize SDL\n"); 
     return 1; 
    } 

    /* seed random number generator */ 
    srand(time(NULL)); 

    /* create window and renderer */ 
    window = 
     SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 
         SDL_WINDOW_OPENGL); 
    if (!window) { 
     printf("Could not initialize Window\n"); 
     return 1; 
    } 

    renderer = SDL_CreateRenderer(window, -1, 0); 
    if (!renderer) { 
     printf("Could not create renderer\n"); 
     return 1; 
    } 

    Rectangles rectangles(SCREEN_WIDTH, SCREEN_HEIGHT); 

    /* Enter render loop, waiting for user to quit */ 
    done = 0; 
    while (!done) { 
     while (SDL_PollEvent(&event)) { 
      if (event.type == SDL_QUIT) { 
       done = 1; 
      } 
     } 


     rectangles.render(renderer); 

     SDL_Delay(1); 
    } 

    /* shutdown SDL */ 
    SDL_Quit(); 

    return 0; 
} 

rectangles.h

#ifndef RECTANGLES_H 
#define RECTANGLES_H 

class Rectangles { 
public: 
    int screenWidth; 
    int screenHeight; 

    Rectangles(int width, int height); 
    void render(SDL_Renderer *renderer); 
    int randomInt(int min, int max); 
}; 

#endif 

rectangles.cpp

#include "SDL.h" 
#include "Rectangles.h" 

Rectangles::Rectangles(int width, int height) 
{ 
    screenWidth = width; 
    screenHeight = height; 

    SDL_Log("Initializing rectangles!"); 
} 

int Rectangles::randomInt(int min, int max) 
{ 
    return min + rand() % (max - min + 1); 
} 

void Rectangles::render(SDL_Renderer *renderer) 
{ 

    Uint8 r, g, b; 

    /* Clear the screen */ 
    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); 
    SDL_RenderClear(renderer); 

    /* Come up with a random rectangle */ 
    SDL_Rect rect; 
    rect.w = randomInt(64, 128); 
    rect.h = randomInt(64, 128); 
    rect.x = randomInt(0, screenWidth); 
    rect.y = randomInt(0, screenHeight); 

    /* Come up with a random color */ 
    r = randomInt(50, 255); 
    g = randomInt(50, 255); 
    b = randomInt(50, 255); 
    SDL_SetRenderDrawColor(renderer, r, g, b, 255); 

    /* Fill the rectangle in the color */ 
    SDL_RenderFillRect(renderer, &rect); 

    /* update screen */ 
    SDL_RenderPresent(renderer); 
} 

我正在使用的代碼稍微從一個教程改性關於使用SDL 。我將用於繪製矩形的部分從main.cpp中的函數移動到它們自己的類中。這在Xcode(iOS)和Eclipse(Android)中編譯並運行良好。我加了rectangles.h文件的位置在Visual Studio中的「附加包含目錄」選項,但它提供了以下錯誤:

Error 2 error LNK2019: unresolved external symbol "public: __thiscall Rectangles::Rectangles(int,int)" ([email protected]@[email protected]@Z) referenced in function _SDL_main Z:\Desktop\SDLWinRTApplication\SDLWinRTApplication\main.obj SDLWinRTApplication 
Error 3 error LNK2019: unresolved external symbol "public: void __thiscall Rectangles::render(struct SDL_Renderer *)" ([email protected]@@[email protected]@@Z) referenced in function _SDL_main Z:\Desktop\SDLWinRTApplication\SDLWinRTApplication\main.obj SDLWinRTApplication 
Error 4 error LNK1120: 2 unresolved externals Z:\Desktop\SDLWinRTApplication\Debug\SDLWinRTApplication\SDLWinRTApplication.exe SDLWinRTApplication 

我已經看了好幾個答案,指示它是什麼毛病我的代碼,但我看不出我做錯了什麼?還是有一個額外的步驟,我需要在Visual Studio中做出這個工作?

最終目標是爲每個平臺設置單獨的項目,這需要一個實際「遊戲」存在的公共庫,這樣我就不會爲每個平臺來回複製文件(先前詢問過它的問題here)。 非常感謝。

編輯: 根據建議,我已經嘗試再次在Visual Studio項目解決方案中添加rectangles.cpp文件。然而,我然後得到了以下的錯誤,我不能讓頭或尾巴。

Error 2 error LNK2038: mismatch detected for 'vccorlib_lib_should_be_specified_before_msvcrt_lib_to_linker': value '1' doesn't match value '0' in MSVCRTD.lib(appinit.obj) Z:\Desktop\SDLWinRTApplication\SDLWinRTApplication\vccorlibd.lib(compiler.obj) SDLWinRTApplication 
Error 3 error LNK2005: ___crtWinrtInitType already defined in MSVCRTD.lib(appinit.obj) Z:\Desktop\SDLWinRTApplication\SDLWinRTApplication\vccorlibd.lib(compiler.obj) SDLWinRTApplication 
Error 4 error LNK1169: one or more multiply defined symbols found Z:\Desktop\SDLWinRTApplication\Debug\SDLWinRTApplication\SDLWinRTApplication.exe SDLWinRTApplication 

EDIT2: 如果我從「附加包含目錄」刪除提及rectangles.h的位置,並添加rectangles.h的項目解決方案旁邊的rectangles.cpp文件,然後我得到:

Error 1 error C1083: Cannot open include file: 'Rectangles.h': No such file or directory z:\desktop\sdlwinrtapplication\sdlwinrtapplication\main.cpp 8 1 SDLWinRTApplication 

按照問題here,Visual Studio中應該能夠找到,當它被添加到項目的根頭文件?

+0

是您的Visual Studio項目的rectangles.cpp部分嗎?看起來構建過程並沒有構建這個過程。 – drescherjm

+2

可能重複[什麼是未定義的引用/無法解析的外部符號錯誤,我該如何解決它?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-符號錯誤和怎麼辦我修復) – PlasmaHH

+0

不,它不是。我曾嘗試通過各種方法添加它,但它似乎只會產生更奇怪的錯誤(「找到一個或多個多個定義的符號」或另一個說明找不到rectangles.h)。爲這種情況添加rectangles.cpp的正確方法是什麼? – Fourjays

回答

0

您必須通過向rectangles.h添加一些內容來「導出」您的Rectangle類的公共接口。說明在此頁面上「向動態鏈接庫添加一個類」: https://msdn.microsoft.com/en-us/library/ms235636.aspx

還要確保所有內容都是針對同一個平臺的;例如所有的x64或所有的Win32。要查看解決方案中每個項目的平臺,請右鍵單擊該解決方案,然後單擊「配置屬性」。

相關問題