I'm a beginner at C++ programming (worked with PHP for years) and am having some trouble with getting Visual Studio Express 2013 to compile some code that works fine in both Xcode and Eclipse.
So the setup is this. I'm aiming to create a game for phones and tablets using SDL 2.0. I'm working a Mac (using a VM for Windows) and have so far successfully got initial test projects working in both Xcode and Eclipse. Both projects link to a common library created using Xcode (for cross-platform development, so I code once for all platforms). I am now trying to add the appropriate Visual Studio project so that I can also compile it for Windows 8/WP8.
Following the instructions here I have successfully managed to get the basic test on that page working, proving that SDL is functioning in Windows 8. However, I am now struggling to link my common library to the project in Visual Studio, which keeps giving me errors. First the code.
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);
}
The code I'm using is slightly modified from a tutorial on using the SDL. I moved the sections for drawing the rectangles from a function in main.cpp to their own class. This compiles and runs fine in both Xcode (iOS) and Eclipse (Android). I added the location of the rectangles.h file to the "Additional Include Directories" option in Visual Studio, but it gives the following errors:
Error 2 error LNK2019: unresolved external symbol "public: __thiscall Rectangles::Rectangles(int,int)" (??0Rectangles@@QAE@HH@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 *)" (?render@Rectangles@@QAEXPAUSDL_Renderer@@@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
I've looked at several answers that indicate it is something wrong with my code, but I can't see what I've done wrong? Or is there an additional step I need to make in Visual Studio to get this working?
The end goal is to have individual projects for each platform, that call on a common library where the actual "game" exists so I'm not copying files back and forth for each platform (asked about it previously here). Many thanks.
EDIT: As per advice, I have tried adding the rectangles.cpp file to the project solution in Visual Studio again. However, I then get the following errors that I can't make head nor tail of.
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: If I remove the reference to the location of rectangles.h from the "Additional Include Directories", and add the rectangles.h to the project solution alongside the rectangles.cpp file, I then get:
Error 1 error C1083: Cannot open include file: 'Rectangles.h': No such file or directory z:\desktop\sdlwinrtapplication\sdlwinrtapplication\main.cpp 8 1 SDLWinRTApplication
According to the question here, Visual Studio should be able to find the header file when it is added to the project root?