2014-06-11 60 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。要查看解决方案中每个项目的平台,请右键单击该解决方案,然后单击“配置属性”。

相关问题