Link problem when compiling from terminal

Hi!

I successfully downloaded and built GLFW.

Then I created a file glfw_test.cpp with the following statements.

#include "glfw-3.2.1\include\GLFW\glfw3.h"

int main () {
	// glfwInit();
	return 0;
}

And I compiled the program with the following instruction.

cl glfw_test.cpp glfw-3.2.1\build\src\release\glfw3.lib

glfw_test.cpp and the glfw-3.2.1 folder are placed in the same parent folder, and the glfw-3.2.1\build subfolder is where I built GLFW.

Compilation finishes silently.

However, when I uncomment the call to glfwInit() and recompile the program with the same instruction above, the compiler reports several problems in the linking phase.

Microsoft (R) C/C++ Optimizing Compiler Version 19.11.25506 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:glfw_test.exe 
glfw_test.obj 
glfw-3.2.1\build\src\Release\glfw3.lib 
LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library
glfw3.lib(init.obj) : warning LNK4217: locally defined symbol ___stdio_common_vsprintf imported in function __glfwInputError
glfw3.lib(wgl_context.obj) : warning LNK4049: locally defined symbol _free imported
glfw3.lib(egl_context.obj) : warning LNK4049: locally defined symbol _free imported
glfw3.lib(monitor.obj) : warning LNK4049: locally defined symbol _free imported
glfw3.lib(vulkan.obj) : warning LNK4049: locally defined symbol _free imported
glfw3.lib(win32_window.obj) : warning LNK4217: locally defined symbol _free imported in function __glfwPlatformSetWindowIcon
glfw3.lib(win32_joystick.obj) : warning LNK4049: locally defined symbol _free imported
glfw3.lib(window.obj) : warning LNK4217: locally defined symbol _free imported in function _glfwDestroyWindow
glfw3.lib(input.obj) : warning LNK4049: locally defined symbol _free imported
glfw3.lib(win32_init.obj) : warning LNK4049: locally defined symbol _free imported
glfw3.lib(win32_monitor.obj) : warning LNK4049: locally defined symbol _free imported
glfw3.lib(monitor.obj) : warning LNK4217: locally defined symbol _qsort imported in function _refreshVideoModes
glfw3.lib(win32_joystick.obj) : warning LNK4049: locally defined symbol _qsort imported
glfw3.lib(wgl_context.obj) : error LNK2019: unresolved external symbol __imp__calloc referenced in function __glfwInitWGL
glfw3.lib(egl_context.obj) : error LNK2001: unresolved external symbol __imp__calloc
glfw3.lib(monitor.obj) : error LNK2001: unresolved external symbol __imp__calloc
glfw3.lib(vulkan.obj) : error LNK2001: unresolved external symbol __imp__calloc
glfw3.lib(win32_window.obj) : error LNK2001: unresolved external symbol __imp__calloc
...

Thanks for your help!

===== EDIT =====

Based on what’s written here, I also linked with opengl32.

So compilation instruction becomes.

cl glfw_test.cpp glfw-3.2.1\build\src\Release\glfw3.lib "C:\Program Files (x86)\Windows Kits\10\Lib\10.0.15063.0\um\x86\OpenGL32.Lib"

But even so, the compiler returns the list of errors above.

The first link warning is because you’ve compiled glfw with a different version of the run-time libraries than the default. You didn’t mention how you compiled GLFW, but using the same run-time library option could help.

Note that you can just specify opengl32.lib, the compiler should know where to find it.

1 Like

I downloaded and unzipped the source package glfw-3.2.1.zip, then

cd glfw-3.2.1
mkdir build
cd build
cmake ..

Finally, I opened bulid\GLFW.sln with Visual Studio and built all.

I’m sorry I’m afraid I did not understand your suggestion. :frowning: Also that is “just” a warning, while there are errors below!

Thanks for the tip! :wink:

The errors are for functions in the standard run-time, and the warning shows that you have a conflict. To resolve you’ll need /MD, but you also need other libraries when building from the command line. People usually start using the visual studio compiler itself, and a good starting point is modifying one of the examples which you should see in the GLFW.sln when opened with VS.

Building from source from github using cmake as you did, then from the Visual Studio I opened a VS command prompt using Tools->Visual Studio Command Prompt. Then I changed dir to the source file, and built your sample from the command line using the same link settings as in the examples plus opengl32.lib (note you don’t need the later for your example as it stands, but you will if you use OpenGL functions).

I then removed a few libraries you don’t need. The command line I used is:

cl /MD glfw_test.cpp gdi32.lib opengl32.lib kernel32.lib user32.lib shell32.lib  glfw-3.2.1\build\src\release\glfw3.lib

This worked.

2 Likes

Thanks for the help!

I’ve been able to compile and run a simple program that creates an empty window.

#include <iostream>
using namespace std;

#include "glfw-3.2.1\include\GLFW\glfw3.h"

void glfwErrorCallbackFunction(int error, const char* description);

int main() {
	// set a callback function for error handling
	glfwSetErrorCallback(glfwErrorCallbackFunction);
	glfwInit();

	GLFWwindow* window = glfwCreateWindow(400, 400, "window title", NULL, NULL);
	glfwMakeContextCurrent(window);

	while (!glfwWindowShouldClose(window)) {
		glfwWaitEvents();
		cout << "some event\n";
	}

	glfwDestroyWindow(window);
	glfwTerminate();

	return 0;
}

void glfwErrorCallbackFunction(int error, const char* description) {
	cout << "GLFW error #" << error << ": " << description << '\n';
}

Now I would like to start rendering something “useful”, such as a uniform background. :slight_smile:

I started writing the procedure to compile the shaders, but the compiler complains of some OpenGL API names, such as glCreateShader or GL_VERTEX_SHADER.

I noticed that in the GL.h header file that is supposedly included in the source when I include glfw3.h, these names do not appear. Also in a comment in GL.h appears a copyright dated 1996! Is this the problem? Why is there such an old version of this file? And how can I access the features introduced by the latest versions of OpenGL?

===== EDIT =====

Ok I think I’ve solved: I also had to include the GL3W library to bypass the problem. :wink:

Glad to hear you’ve solved your issues.

If you have any more do post here, but it might be best to open a new thread to keep things well organised so people can more easily search for answers to questions which have already been asked.

Good luck with your rendering!

2 Likes