仅在Visual Studio中无法解析的外部符号
Unresolved external symbol in Visual Studio only?
我是c++编程的初学者(使用PHP多年),并且在获得Visual Studio Express 2013编译一些在Xcode和Eclipse中都能正常工作的代码时遇到了一些麻烦。
是这样的。我的目标是使用SDL 2.0为手机和平板电脑创造一款游戏。我在Mac上工作(使用Windows的VM),到目前为止已经成功地在Xcode和Eclipse中获得了初始测试项目。这两个项目都链接到使用Xcode创建的公共库(用于跨平台开发,所以我为所有平台编写了一次代码)。我现在正在尝试添加适当的Visual Studio项目,以便我也可以为Windows 8/WP8编译它。
按照这里的说明,我已经成功地在该页上进行了基本测试,证明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 SDLn");
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 Windown");
return 1;
}
renderer = SDL_CreateRenderer(window, -1, 0);
if (!renderer) {
printf("Could not create renderern");
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)上都可以很好地编译和运行。我将矩形文件的位置添加到Visual Studio中的"附加包含目录"选项中,但它给出了以下错误:
Error 2 error LNK2019: unresolved external symbol "public: __thiscall Rectangles::Rectangles(int,int)" (??0Rectangles@@QAE@HH@Z) referenced in function _SDL_main Z:DesktopSDLWinRTApplicationSDLWinRTApplicationmain.obj SDLWinRTApplication
Error 3 error LNK2019: unresolved external symbol "public: void __thiscall Rectangles::render(struct SDL_Renderer *)" (?render@Rectangles@@QAEXPAUSDL_Renderer@@@Z) referenced in function _SDL_main Z:DesktopSDLWinRTApplicationSDLWinRTApplicationmain.obj SDLWinRTApplication
Error 4 error LNK1120: 2 unresolved externals Z:DesktopSDLWinRTApplicationDebugSDLWinRTApplicationSDLWinRTApplication.exe SDLWinRTApplication
我看了几个答案,表明这是我的代码有问题,但我看不出我做错了什么?或者有一个额外的步骤,我需要在Visual Studio中得到这个工作?
最终目标是每个平台都有单独的项目,调用一个真正的"游戏"存在的公共库,这样我就不用为每个平台来回复制文件了。多谢。
编辑:根据建议,我再次尝试将rectangles.cpp文件添加到Visual Studio中的项目解决方案中。然而,我得到以下错误,我不能使头或尾
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:DesktopSDLWinRTApplicationSDLWinRTApplicationvccorlibd.lib(compiler.obj) SDLWinRTApplication
Error 3 error LNK2005: ___crtWinrtInitType already defined in MSVCRTD.lib(appinit.obj) Z:DesktopSDLWinRTApplicationSDLWinRTApplicationvccorlibd.lib(compiler.obj) SDLWinRTApplication
Error 4 error LNK1169: one or more multiply defined symbols found Z:DesktopSDLWinRTApplicationDebugSDLWinRTApplicationSDLWinRTApplication.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:desktopsdlwinrtapplicationsdlwinrtapplicationmain.cpp 8 1 SDLWinRTApplication
根据这里的问题,Visual Studio应该能够找到头文件时,它被添加到项目根?
您必须通过在rectangles.h中添加一些内容来"导出"Rectangle类的公共接口。说明在本页的"向动态链接库添加类"中:https://msdn.microsoft.com/en-us/library/ms235636.aspx
还要确保所有内容都是针对同一个平台;例如所有x64或所有Win32。要查看解决方案中每个项目的平台,右键单击解决方案,单击"配置属性"。
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- 未解析的外部符号_MsiLocateComponentW@12.
- 在C++中使用 gRPC 时未解析的外部符号
- LibPrivoxy: 未解析的外部符号 __declspec(dllimport) int __stdcall Sta
- 尝试从 XamlApplication 编译 C++/WinRT 空白应用程序时,我收到未解析的外部符号 winrt_make_*
- 我收到错误LNK2001:未解析的外部符号(C++代码)
- 可视抽象类 c++(错误 LNK 2001:未解析的外部符号)
- 如何摆脱C++中未解析的外部符号"private: static char"错误?
- 当 lib 已添加到其他依赖项时,如何在 VS 中调试未解析的外部符号错误
- 仅在少数方法(静态或共享库)中解析的外部符号
- 未解析的外部符号"__declspec(dllimport)与 Spdlog c++ 库
- 函数 _main (OPENGL C++) 中引用的未解析的外部符号 ________
- 错误LNK2019未解析的外部符号"public: __thiscall SLinkList<char>::SLinkList<char>(void)"
- 使用静态 constexpr 成员的未解析外部符号
- 引入参数化构造函数后显示 LNK 2019 未解析外部符号的代码错误
- Dlib LNK2001未解析的外部符号USER_ERROR__consistent_build_configurati
- 矛盾的未解决的外部符号+未使用的库与VS2017和FFMPEG 4
- 链接器错误:切换到unicode生成给出:未解析的外部符号WinMain
- 类模板上一个特定函数的未解析外部符号
- LNK2001:未解析的外部符号public:static类std::vector