Compiling and linking libraries

Mike Parker via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Nov 16 08:05:06 PST 2016


On Wednesday, 16 November 2016 at 14:59:40 UTC, Edwin van Leeuwen 
wrote:
>
>
> If you are happy to use dub I would just add the GL library as 
> a dependency to my dub.json file. Then if you call dub it will 
> download and compile the necessary file.
>
> Example dub.json file:
> ```
> {
> 	"name": "myWindow",
> 	"authors": [
> 		"Darren"
> 	],
> 	"description": "A minimal D application.",
> 	"copyright": "Copyright © 2016, Darren",
>         "dependencies": {
>                 "derelict-gl3": "~>2.0.0-alpha.2"
>         }
> }
> ```
>

You'll also want the derelict-glfw3 binding so that you can 
create the window and OpenGL context in the same way as the 
learnopengl.com tutorials. Moreover, as the maintainer of 
Derelict, I advise against using the 'alpha' versions of the 
Derelict libraries for now given that this is a learning 
exercise. Your dependencies should look more like this:

"dependencies": {
     "derelict-gl3": "~>1.0.19"
     "derelict-glfw3" : "~>3.1.1"
}

You don't need to worry about CMake, as DUB fills that role (and 
more). You also don't need to worry about GLEW, which is strictly 
a C and C++ thing. DerelictGL3 will perform the same function. 
Just make sure you have the latest GLFW DLL in your project 
directory on Windows, or have installed it in the system 
directories on Mac or Linux. On Windows, you can save yourself 
some trouble by structuring your tutorial directories something 
like this:

- learnogl
- bin
-- glfw3.dll
-- HelloWindow
--- dub.json
-- HelloTriangle
--- dub.json

Then, somewhere in each dub.json, add the following line:

"targetPath": "../bin"

Make sure each dub.json has a different "name". On Mac or Linux, 
the GLFW shared library will be in on of the system directories 
after you install it, so you don't need to worry about it.

Here are some major points you need to be aware of that will 
differ from the tutorial:

* You don't need to link with any libraries. All of the Derelict 
packages are dynamic bindings by default [1], so there are no 
link-time dependencies. That means the share libraries (DLLs) 
need to be loaded manually. Before you call any OpenGL or GLFW 
functions, you need the following two lines (preferably at the 
top of your main function to play it safe):

DerelictGL3.load();
DerelictGLFW3.load();

This will load the libraries. For GLFW, that's all you need to 
use it. For OpenGL, this will only load the the functions up to 
1.1. In order to load the rest, you first need to create a 
context. In the learnopengl.com tutorial called 'Hello Window', 
completely ignore the following:

glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
{
     std::cout << "Failed to initialize GLEW" << std::endl;
     return -1;
}

Remember, you don't need GLEW. Instead, replace it with this:

DerelictGL3.reload();

Make sure it's after the call to glfwMakeContextCurrent. With 
that, you'll be able to mostly use the same C code the tutorial 
uses in your D programs. There are a small number of subtle 
differences that will cause the D compiler to complain (mostly 
about implicit conversions), but I won't list them here because 
that will just confuse you. When you encounter them, if it isn't 
obvious what's going on, you can always come back here and ask 
for help.

[1] http://derelictorg.github.io/bindings/


More information about the Digitalmars-d-learn mailing list