opengl lnk 2019 only one function not found - c++

i have seen this error a lot in the setup of opengl, but now that it is working, there is this error now. all the rest is working just this and glClearColor isnt
i think that all the commands in gl.h and glew.h might not be working i havent tested yet, and yes they are added to the project as they should be, the libraries are loading well, but i just cant call functions from them. i was using glut and glew before with no problem, what might have happened in here?
#include<map>
#include<iostream>
#include<stdio.h>
#include<chrono>
#include<stdlib.h>
#define WIDTH 320
#define HEIGHT 200
struct key {
};
GLFWwindow* window;
GLFWmonitor* monitor;
bool running = 1, windowed;
std::map<int, key> keyMap;
void update(){}
void input(){}
void draw(){
glClear(GL_COLOR_BUFFER_BIT);
}
int main() {
glfwWindowHint(GLFW_SAMPLES, 1);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
if (!glfwInit()) fprintf(stderr, "opengl error\n");
window = glfwCreateWindow(WIDTH, HEIGHT, "end", NULL, NULL);
if (window == NULL) {std::cout << "window error\n" << std::endl; glfwTerminate();}
glfwMakeContextCurrent(window);
monitor = glfwGetPrimaryMonitor();
while (running)
{
update();
input();
draw();
}
glfwDestroyWindow(window);
glfwTerminate();
}
the errors are just these:
warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library
error LNK2019: unresolved external symbol __imp__wglGetProcAddress#4 referenced in function "void __cdecl draw(void)" (?draw##YAXXZ)
error LNK1120: 1 unresolved externals

Related

Seemingly Random LNK2005 Errors With GLFW3

I'm getting the error LNK2005 for seemingly no reason, or at least none that can I can identify. Essentially, using the following code I can compile without issue.
test.h
#define GLEW_STATIC
#include <GL\glew.h>
#include <GLFW\glfw3.h>
namespace test
{
//Objects and variables
GLFWwindow* window;
//Function prototypes
bool Initialize();
}
test.cpp
#include "test.h"
#include <iostream>
bool test::Initialize()
{
std::cout << "Initializing GLFW: OpenGL version 3.3 \n";
//Initialize GLFW
glfwInit();
//Set window properties (version 3.3, core profile, not resizeable)
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
//Create Window
window = glfwCreateWindow(800, 800, "Learn OpenGL", nullptr, nullptr);
if (window = nullptr)
{
std::cout << "Failed to create GLFW window \n";
glfwTerminate();
return false;
}
return true;
}
int main()
{
test::Initialize();
return 0;
}
However, when compiling nearly the exact same thing (http://pastebin.com/VpPep9pM), along with some other code, it gives the errors:
Error LNK2005 "struct GLFWwindow * window" (?window##3PAUGLFWwindow##A) already defined in Main.obj OpenGL D:\Users\Matthew\documents\visual studio 2015\Projects\OpenGL\OpenGL\System.obj
Error LNK2005 "struct GLFWwindow * System::window" (?window#System##3PAUGLFWwindow##A) already defined in Main.obj OpenGL D:\Users\Matthew\documents\visual studio 2015\Projects\OpenGL\OpenGL\System.obj
Error LNK1169 one or more multiply defined symbols found OpenGL D:\Users\Matthew\documents\visual studio 2015\Projects\OpenGL\Debug\OpenGL.exe
So, I would like to know what causes the errors, I'm assuming it has something to do with the "context".
In your header file, you are defining you variable, when it needs to be declared in the header and defined in one source file.
In test.h
namespace test {
extern GLFWwindow* window;
}
and in main.cpp
namespace test {
GLFWwindow* window;
}
Without the extern in the header, you create one instance of test::window in every source file that includes it, which is a problem if there are two or more definitions.

Visual Studio 2013 and OpenGL

I have recently purchased a new laptop and got a new version of VS from my school. And I'm having a little trouble setting up my libraries.
I created a basic SDL_Window, as well as a SDL_GLContext.
I'm able to include my libraries and run the program, but I can't call functions like glClearColor, or glGetString(GL_VERSION). I get a rather strage warning and an error that I have never seen before, I'm guessing it's related to the 2013 version?
I have tried ignoring all specific default libraries, as well as trying to change the programtype (Multithreaded DLL, those 4).
And I have made sure all the dll-files are in place in my system folder.
What makes me wonder is the fact that glewInit() works, but not glClearColor() etc...
Output:
1>MSVCRTD.lib(cinitexe.obj) : warning LNK4098: defaultlib 'msvcrt.lib'
conflicts with use of other libs; use /NODEFAULTLIB:library
1>Window.obj : error LNK2019: unresolved external symbol
__imp__glClearColor#16 referenced in function "public: void __thiscall Window::initOpenGL(void)" (?initOpenGL#Window##QAEXXZ)
1>C:\Users\Aleksander\documents\visual studio 2013\Projects\OpenGL
Test\Debug\OpenGL Test.exe : fatal error LNK1120: 1 unresolved
externals
Header:
#pragma once
#include <SDL.h> //Tested OK
#include <SDL_image.h> //Tested OK
#include <SDL_mixer.h> //Tested OK
#include <SDL_net.h> //Tested OK
#include <OpenGL\glew.h> //?
#include <freeglut\freeglut.h> //?
#include <gl\GL.h> //?
#include <glm\glm.hpp> //Tested OK
#include <iostream> //Standard OK
#include <Box2D\Box2D.h> //Tested OK
using namespace std;
class Window {
SDL_Window *window;
SDL_GLContext context;
bool quit;
public:
Window();
~Window();
void initSDL();
void initOpenGL();
void run();
};
Source:
#include "Window.h"
Window::Window() {
printf("Starting window...\n");
//Initialize window
quit = false;
initSDL();
initOpenGL();
//Run window
run();
}
Window::~Window() {
printf("Closing window...\n");
//Clear window memory
SDL_GL_DeleteContext(context);
SDL_DestroyWindow(window);
//Close window library
SDL_Quit();
}
void Window::initSDL() {
if (SDL_Init(SDL_INIT_EVERYTHING) == -1) {
quit = true;
printf("Unable to initialize SDL!\n");
}
else {
//Create window
const char *title = "OpenGL Test";
int pos = SDL_WINDOWPOS_CENTERED;
int w = 800;
int h = 600;
Uint32 flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
window = SDL_CreateWindow(title, pos, pos, w, h, flags);
//Create OpenGL context
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
context = SDL_GL_CreateContext(window);
}
}
void Window::initOpenGL() {
GLenum init = glewInit();
if (init != GLEW_OK) {
quit = true;
printf("Unable to initialize OpenGL!\n");
printf("Error: %s!\n", glewGetErrorString(init));
}
else {
//printf("Vendor: %s\n", glGetString(GL_VENDOR));
//printf("Version: %s\n", glGetString(GL_VERSION));
//printf("Renderer: %s\n", glGetString(GL_RENDERER));
}
}
void Window::run() {
printf("Window started running!\n");
SDL_Event event;
while (!quit) {
SDL_GL_SwapWindow(window);
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
quit = true;
}
}
}
}

Unresolved external symbol in Visual Studio only?

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?
You have to "export" the public interface of your Rectangle class by adding something to rectangles.h. Instructions are at "add a class to the dynamic link library" on this page:
https://msdn.microsoft.com/en-us/library/ms235636.aspx
Also make sure that everything is for the same platform; all x64 or all Win32 for example. To see the platform of each project in the solution, right click the solution and the click on "Configuration Properties".

Link Errors in Simple SDL Engine

I am trying to build a simple game engine with C++ and SDL. I am going through and trying to take the things that I know work in one big .cpp file, and organizing them into more logical patterns. For example, having a Game class, that contains a Screen or Engine class, a Logic class, an Entities class, etc. Obviously doesn't need to be super advanced as I'm just trying to get a handle for the first time on setting up things logically. Unfortunately I get the linker errors:
1>game.obj : error LNK2019: unresolved external symbol "public: __thiscall Screen::~Screen(void)" (??1Screen##QAE#XZ) referenced in function "public: __thiscall Game::Game(void)" (??0Game##QAE#XZ)
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall Game::~Game(void)" (??1Game##QAE#XZ) referenced in function _SDL_main
And the SDL is definitely set up properly, because the code executes just fine when everything is in one big file.
So far I have a Game class, and a Screen class.
game.h
#ifndef GAME_H
#define GAME_H
// Includes
#include "screen.h"
// SDL specific includes
#include "SDL.h"
class Game
{
private:
Screen screen;
public:
Game();
~Game();
};
#endif
game.cpp
#include "game.h"
Game::Game()
{
Screen screen;
}
screen.h
#ifndef SCREEN_H
#define SCREEN_H
#include "SDL.h"
class Screen
{
private:
// Screen dimension variables
int SCREEN_WIDTH, SCREEN_HEIGHT;
// --SDL object variables--
// The window we'll be rendering to
SDL_Window * window;
// The surface contained by the window
SDL_Surface * screenSurface;
public:
Screen();
~Screen();
// Functions for calling SDL objects
SDL_Window *getSDLWindow( void );
SDL_Surface *getSDLSurface( void );
};
#endif
screen.cpp
#include "screen.h"
#include <stdio.h>
Screen::Screen()
{
// Initialize window and SDL surface to null
window = NULL;
screenSurface = NULL;
// Initialize screen dimensions
SCREEN_WIDTH = 800;
SCREEN_HEIGHT = 600;
// Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError() );
}
else
{
// Create a window
window = SDL_CreateWindow( "The First Mover", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
if (window == NULL)
{
printf ("Window could not be created! SDL Error: %s\n", SDL_GetError() );
}
else
{
// Get window surface
screenSurface = SDL_GetWindowSurface( window );
// Fill the surface white
SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0xFF, 0xFF, 0xFF) );
}
}
}
Any assistance appreciated! Also, if anyone has any ideas on how to better organize what I'm trying to do, feel free to comment!
Since you have declared the destructors for Screen and Game you need to provide definitions for them too. Add this in the Game.cpp and Screen.cpp files:
Game::~Game(){
// what ever you need in the destructor
}
Screen::~Screen(){
// what ever you need in the destructor
}
If you hadn't declared the destructors the compiler would have generated an implicitly for you.

GLEW Linking Error. error LNK2019

I'm using Visual Studio 2013 and am new to C++. I installed GLEW and freeglut. I try to build my file "main.c", and I receive this:
1>------ Build started: Project: testGlut1, Configuration: Debug Win32 ------
1> main.c
1>main.obj : error LNK2019: unresolved external symbol __imp__glewInit#0 referenced function _Initialize
1>main.obj : error LNK2019: unresolved external symbol __imp__glewGetErrorString#4 referenced in function _Initialize
1>C:\Users\User\documents\visual studio 2013\Projects\testGlut1\Debug\testGlut1.exe : fatal error LNK1120: 2 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I read through a similar issue, however the solution didn't work for me, in fact, nothing changed. Also, I only have 2 unresolved externals whereas the aforementioned issue showed 16 unresolved externals. Why? Why are those two errors isolated as such?
The source code for my program:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <GL/glew.h>
#include <GL/freeglut.h>
#define WINDOW_TITLE_PREFIX "Chapter 1"
int CurrentWidth = 800,
CurrentHeight = 600,
WindowHandle = 0;
unsigned FrameCount = 0;
void Initialize(int, char*[]);
void InitWindow(int, char*[]);
void ResizeFunction(int, int);
void RenderFunction(void);
void TimerFunction(int);
void IdleFunction(void);
int main(int argc, char* argv[])
{
Initialize(argc, argv);
glutMainLoop();
exit(EXIT_SUCCESS);
}
void Initialize(int argc, char* argv[])
{
GLenum GlewInitResult;
InitWindow(argc, argv);
GlewInitResult = glewInit();
if (GLEW_OK != GlewInitResult) {
fprintf(
stderr,
"ERROR: %s\n",
glewGetErrorString(GlewInitResult)
);
exit(EXIT_FAILURE);
}
fprintf(
stdout,
"INFO: OpenGL Version: %s\n",
glGetString(GL_VERSION)
);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}
void InitWindow(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitContextVersion(4, 0);
glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutSetOption(
GLUT_ACTION_ON_WINDOW_CLOSE,
GLUT_ACTION_GLUTMAINLOOP_RETURNS
);
glutInitWindowSize(CurrentWidth, CurrentHeight);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
WindowHandle = glutCreateWindow(WINDOW_TITLE_PREFIX);
if (WindowHandle < 1) {
fprintf(
stderr,
"ERROR: Could not create a new rendering window.\n"
);
exit(EXIT_FAILURE);
}
glutReshapeFunc(ResizeFunction);
glutDisplayFunc(RenderFunction);
glutIdleFunc(IdleFunction);
glutTimerFunc(0, TimerFunction, 0);
}
void ResizeFunction(int Width, int Height)
{
CurrentWidth = Width;
CurrentHeight = Height;
glViewport(0, 0, CurrentWidth, CurrentHeight);
}
void RenderFunction(void)
{
++FrameCount;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
glutPostRedisplay();
}
void IdleFunction(void)
{
glutPostRedisplay();
}
void TimerFunction(int Value)
{
if (0 != Value) {
char* TempString = (char*)
malloc(512 + strlen(WINDOW_TITLE_PREFIX));
sprintf(
TempString,
"%s: %d Frames Per Second # %d x %d",
WINDOW_TITLE_PREFIX,
FrameCount * 4,
CurrentWidth,
CurrentHeight
);
glutSetWindowTitle(TempString);
free(TempString);
}
FrameCount = 0;
glutTimerFunc(250, TimerFunction, 1);
}
Here are my additional dependencies:
Here is the Debug folder in MyDocuments:
There were simply incompatibilities between the OS versions of the linked files. I thought that the process was using SysWOW64 to search for the DLL files of GLUT and GLEW since my OS is Windows 64-bit. Anyway, I used the tool, Dependecy Walker 2.2, to track an previously created .exe file and learned that my OS was searching C:\Windows\System32 for the DLL files. That changed everything, annoyingly. Out of frustration in having searched for answers for over a day and a half, I went into C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x86 and swapped the glew32.lib (64-bit) for glew32.lib (32-bit), and voila, I can run my program without any errors. Since I am not an expert, or even practiced in C programming, I can only say that this is a temporary solution for my problem. I'm not sure why the program searches through C:\Windows\System32 rather than C:\Windows\SysWOW64. But, I hope that this solution helps those whom have exprienced a mysterious linker error messages when trying to use GLEW, GLUT, and Microsoft Visual Studio.