# Compiling test program for Linux x86

**URL:** <https://discourse.glfw.org/t/compiling-test-program-for-linux-x86/1369>\
**Category:** support\
**Created:** [July 22, 2019, 3:43pm UTC](https://discourse.glfw.org/t/compiling-test-program-for-linux-x86/1369 "2019-07-22T15:43:15Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![pvorisek25](https://avatars.discourse-cdn.com/v4/letter/p/ce7236/32.png) [@pvorisek25](https://discourse.glfw.org/u/pvorisek25)\
**Post date:** [July 22, 2019, 3:43pm UTC](https://discourse.glfw.org/t/compiling-test-program-for-linux-x86/1369/1 "2019-07-22T15:43:15Z")

</div>

Hi, again 🙂,

when I compiled static library of GLFW for Linux x86 I tried to make something with that library. I wrote this program (test.cpp):

```
#undef GLFW_DLL
#include <iostream>
#include <GLFW/glfw3.h>

int main()
{
    if (!glfwInit())
        std::cout << "Failed" << std::endl;
    else
        std::cout << "Success" << std::endl;

    return 0;
}

```

… and I wrote this bash script (compile.sh) for easier compiling:

```
#!/bin/bash
g++ -static -I./Include -L./Libs/Linux-x86/GLFW test.cpp -o test -l:glfw3.a -m32

```

I am using g++ compiler (version: 7.4.0) on Linux Mint 19.1 Cinnamon (Linux core version: 4.15.0-45-generic).

When I try to compile test.cpp I’ll get this output from g++:

```
/tmp/ccWUvEDU.o: In function `main':
test.cpp:(.text+0x1b): undefined reference to `glfwInit'
collect2: error: ld returned 1 exit status

```

I don’t know if I compiled static library correctly so here is link to download compiled static library (Linux x86): [https://drive.google.com/open?id=1DZ1ohO4vkcbtU3g4Q9WkEwJr0jqQiOH7](https://drive.google.com/open?id=1DZ1ohO4vkcbtU3g4Q9WkEwJr0jqQiOH7)

What I am doing wrong?

Thanks for answers

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [July 22, 2019, 4:41pm UTC](https://discourse.glfw.org/t/compiling-test-program-for-linux-x86/1369/2 "2019-07-22T16:41:30Z")

</div>

I’m not used to the syntax you’ve used for compiling, I would be more familiar with:

```auto
g++ -static -I/Include -L/Libs/Linux-x86/GLFW test.cpp -o test -lglfw3 -m32

```

You might want to first try [building glfw through cmake](https://www.glfw.org/docs/latest/compile_guide.html) and then compiling with the library as per [the documentation on building applications](https://www.glfw.org/docs/latest/build_guide.html#build_link_pkgconfig).

---

<div class="post-metadata">

**Author:** ![pvorisek25](https://avatars.discourse-cdn.com/v4/letter/p/ce7236/32.png) [@pvorisek25](https://discourse.glfw.org/u/pvorisek25)\
**Post date:** [July 22, 2019, 5:53pm UTC](https://discourse.glfw.org/t/compiling-test-program-for-linux-x86/1369/3 "2019-07-22T17:53:07Z")

</div>

Thanks for response dougbinks. Your syntax of the compile command does not work because I am using relative path. So I used this one:

```
g++ -static -IInclude -LLibs/Linux-x86/GLFW test.cpp -o test -l:glfw3.a -m32

```

Also I am trying to use static library from my own path. Not from system include path. I am trying to do this because this is how you do it in Windows and I’d like to have same ‘system’ for including libraries on both operating systems. So I do not want to use this:

```
g++ `pkg-config --cflags glfw3` -o test test.cpp `pkg-config --static --libs glfw3`

```

Which is including library from system path. After your reply I tried to compile GLFW lib with this flag:

```
cmake -DBUILD_SHARED_LIBS=ON .

```

But nothing changed and I am still getting this error:

```
/tmp/ccbwFoN9.o: In function `main':
test.cpp:(.text+0x1b): undefined reference to `glfwInit'
collect2: error: ld returned 1 exit status

```

Did you spotted some mistakes that I am doing with linking?

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [July 23, 2019, 1:15pm UTC](https://discourse.glfw.org/t/compiling-test-program-for-linux-x86/1369/4 "2019-07-23T13:15:22Z")

</div>

Sorry I shouldn’t have stripped off the `.` from the paths. My main concern was that the link phrase `-l:glfw3.a` was a bit unusual. Normally `-lglfw3` should find the glfw library from your library path. You shouldn’t need the seperator `:`.

If you’re trying to force the linker to link to your library over a system one using the exact path in the link instruction might help, i.e.: `-l./Libs/Linux-x86/GLFW/libglfw3.a`.

If you’re still stuck I would still advice starting from a known working state - use the cmake approach to create a static library (you don’t have to install it) and example code then you know you have a working library. Go from there.

Note that I also don’t like to install libraries (dependency nightmare).

Also note that you can list the exports of your library using `nm ./Libs/Linux-x86/GLFW/libglfw3.a`.

---

<div class="post-metadata">

**Author:** ![pvorisek25](https://avatars.discourse-cdn.com/v4/letter/p/ce7236/32.png) [@pvorisek25](https://discourse.glfw.org/u/pvorisek25)\
**Post date:** [July 23, 2019, 5:05pm UTC](https://discourse.glfw.org/t/compiling-test-program-for-linux-x86/1369/5 "2019-07-23T17:05:54Z")

</div>

Thank you for reply. Your way of forcing the a compiler to use own library work but I got same output. So I tried to compile static library and examples from source code which failed. I got these errors:

```
-- The C compiler identification is GNU 7.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - not found
CMake Error at /usr/share/cmake-3.10/Modules/FindPackageHandleStandardArgs.cmake:137 (message):
  Could NOT find Threads (missing: Threads_FOUND)
Call Stack (most recent call first):
  /usr/share/cmake-3.10/Modules/FindPackageHandleStandardArgs.cmake:378 (_FPHSA_FAILURE_MESSAGE)
  /usr/share/cmake-3.10/Modules/FindThreads.cmake:205 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
  CMakeLists.txt:63 (find_package)

-- Configuring incomplete, errors occurred!
See also "/home/petr/Dokumenty/GitHub/Nexgo/Nexgo/Dependencies/Building/glfw-master1/CMakeFiles/CMakeOutput.log".
See also "/home/petr/Dokumenty/GitHub/Nexgo/Nexgo/Dependencies/Building/glfw-master1/CMakeFiles/CMakeError.log".

```

Note: I added this line to both CMakeLists.txt (in root folder of GLFW source and in examples folder):

```
set(CMAKE_C_FLAGS “-m32”)

```

So I tried the ‘nm’ command with my existing library and I found that glfwInit is there (is listed as outuput of the command). And finally I tried to use example of GLFW lib from official documentation.

```
#include <GLFW/glfw3.h>
int main(void)
{
    GLFWwindow* window;

   /* Initialize the library */
   if (!glfwInit())
       return -1;

   /* Create a windowed mode window and its OpenGL context */
   window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
   if (!window)
   {
       glfwTerminate();
       return -1;
   }

   /* Make the window's context current */
   glfwMakeContextCurrent(window);

   /* Loop until the user closes the window */
   while (!glfwWindowShouldClose(window))
   {
       /* Render here */
       glClear(GL_COLOR_BUFFER_BIT);

       /* Swap front and back buffers */
       glfwSwapBuffers(window);

       /* Poll for and process events */
       glfwPollEvents();
   }

   glfwTerminate();
   return 0;
} 

```

But I got same output. So I tried to use relative path to my header files of GLFW:

```
#include "Include/GLFW/glfw3.h"

```

But it failed too. Have you got any idea what I am doing bad?

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [July 23, 2019, 5:35pm UTC](https://discourse.glfw.org/t/compiling-test-program-for-linux-x86/1369/6 "2019-07-23T17:35:10Z")

</div>

Trying to compile for 32bit machines on Linux x64 can be awkward. Just to make sure we have a decent starting point can you compile and link without setting the 32bit machine architecture parameter?

For the cmake error it looks like you are missing the 32-bit version of the package for pthreads, you can install the required 32bit packages, see [https://askubuntu.com/questions/522372/installing-32-bit-libraries-on-ubuntu-14-04-lts-64-bit](https://askubuntu.com/questions/522372/installing-32-bit-libraries-on-ubuntu-14-04-lts-64-bit).

---

<div class="post-metadata">

**Author:** ![pvorisek25](https://avatars.discourse-cdn.com/v4/letter/p/ce7236/32.png) [@pvorisek25](https://discourse.glfw.org/u/pvorisek25)\
**Post date:** [July 23, 2019, 7:21pm UTC](https://discourse.glfw.org/t/compiling-test-program-for-linux-x86/1369/7 "2019-07-23T19:21:48Z")

</div>

Thanks for response. I have one question about generating GLFW. In this section ([https://www.glfw.org/docs/latest/compile\_guide.html#compile\_generate](https://www.glfw.org/docs/latest/compile_guide.html#compile_generate)) I have to use CMake to generate some files. So I always use:

```
cmake -D(sometomes some parameters) .

```

But in doc is example without -D parameters (I cought that now).  
In this section ([https://www.glfw.org/docs/latest/compile\_guide.html#compile\_compile](https://www.glfw.org/docs/latest/compile_guide.html#compile_compile)) they are talking again about CMake (now with -D parameters) but CMake (the first one) only generates some ‘Makefile’ and I always execute is with command:

```
make

```

Do you know how did they mean it? And yes I tried download some libs from your link but I had the already. I’ll google more tomorrow.

---

<div class="post-metadata">

**Author:** ![pvorisek25](https://avatars.discourse-cdn.com/v4/letter/p/ce7236/32.png) [@pvorisek25](https://discourse.glfw.org/u/pvorisek25)\
**Post date:** [July 24, 2019, 7:17pm UTC](https://discourse.glfw.org/t/compiling-test-program-for-linux-x86/1369/8 "2019-07-24T19:17:10Z")

</div>

Here is little update:

When I try to compile lib with:

```
sudo cmake -DGLFW_BUILD_EXAMPLES=ON BUILD_SHARED_LIBS=ON .

```

It works and it make GLFW library and build examples without errors. However when I try to execute the examples I get this output:

```
./particles.c.o: 1: ./particles.c.o: Syntax error: "(" unexpected

```

I did not find anything on the internet about this issue. Note: I was not compiling for 32 bit systems. So I tried to build my program (did not work before) with built library (And yeah I removed the flag: “-m32”) but again, it did not work too.

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [July 25, 2019, 1:34pm UTC](https://discourse.glfw.org/t/compiling-test-program-for-linux-x86/1369/9 "2019-07-25T13:34:52Z")

</div>

Some things to correct here, then I’ll run a quick tutorial of what to do:

1. You don’t need use `sudo` to run `cmake`.
2. Each option you want to set needs `-D` in front.
3. `cmake` generates project files, you then need to run `make` to build the project.

Now I’ll run through how I compile glfw and run an example:

I have the directory structure:

/home/doug/projects/glfw/  
/home/doug/projects/glfw\_build/

glfw directory has the code from glfw downloaded from [https://github.com/glfw/glfw](https://github.com/glfw/glfw) (I used git to do so but you could also download the zip file and extract it here. The directory glfw should have the file .CMakeLists.txt file in it.

glfw\_build is a directory I created to use to create a build in.

Then I open a command prompt in glfw\_build and run cmake as follows:

```bash
cd ~/Projects/glfw_build
cmake ../glfw

```

You should see some text output with the last line being:

```auto
-- Build files have been written to: /home/doug/projects/glfw_build

```

I then build the code using make:

```bash
make all

```

You should see a lot of text output with hopefully no errors.

You will have several folders. The folder `src` will have `libglfw3.a` in it, `examples` will have example apps and `tests` will have some tests.

Run:

```bash
./examples/boing

```

You should see a spinning bouncing ball.

---

<div class="post-metadata">

**Author:** ![pvorisek25](https://avatars.discourse-cdn.com/v4/letter/p/ce7236/32.png) [@pvorisek25](https://discourse.glfw.org/u/pvorisek25)\
**Post date:** [July 26, 2019, 9:18am UTC](https://discourse.glfw.org/t/compiling-test-program-for-linux-x86/1369/10 "2019-07-26T09:18:42Z")

</div>

Thank you a lot. Examples and test works (built for 64 bit systems). Yesterday I was trying to compile my program but it failed again with same output. I tried to build my program with flags from test and examples. So I tried to use this:

```
g++ -static test.cpp -o test -IInclude -LLibs/Linux-x86/GLFW/libglfw3.a -lm -lrt -lm -ldl -lX11 -lpthread

```

and it did not work.

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [July 26, 2019, 10:42am UTC](https://discourse.glfw.org/t/compiling-test-program-for-linux-x86/1369/11 "2019-07-26T10:42:20Z")

</div>

I’ll look into this in more depth when I get time, but I think you should remove the `-static` option as this forces gcc to link **all** libraries as static. Since GLFW is already a static library and you’re linking to that you shouldn’t need this option.

See [https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html](https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html)

```auto
-static
On systems that support dynamic linking, this overrides -pie and prevents linking with the shared libraries. On other systems, this option has no effect.

```

---

<div class="post-metadata">

**Author:** ![pvorisek25](https://avatars.discourse-cdn.com/v4/letter/p/ce7236/32.png) [@pvorisek25](https://discourse.glfw.org/u/pvorisek25)\
**Post date:** [July 27, 2019, 7:21am UTC](https://discourse.glfw.org/t/compiling-test-program-for-linux-x86/1369/12 "2019-07-27T07:21:57Z")

</div>

Thank you for response. It does not work even without the -static flag. I am really happy that you are still with me. I am not doing this for job so this can wait. I’ll check this topic every day.

---

<div class="post-metadata">

**Author:** ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)\
**Post date:** [July 27, 2019, 11:11am UTC](https://discourse.glfw.org/t/compiling-test-program-for-linux-x86/1369/13 "2019-07-27T11:11:18Z")

</div>

The following worked for me.

I have the directory structure as before but with a new directory for the example code.

/home/doug/projects/glfw/  
/home/doug/projects/glfw\_build/  
/home/doug/projects/glfw\_example/

My example code is:

```auto
#include <iostream>
#include <GLFW/glfw3.h>

int main()
{
    if (!glfwInit())
        std::cout << "Failed" << std::endl;
    else
        std::cout << "Success" << std::endl;

    return 0;
}

```

In a file called main.cpp

To compile this from a cmd prompty opened in glfw\_example directory I used:

```auto
g++ test.cpp -o test -I../glfw/include -L../glfw_build/src -lglfw3 -ldl -lX11 -lpthread

```

This was on Ubuntu but I think you should have the same requirements on Mint. For OpenGL use just add `-lGL` to link to OpenGL. With that I was able to edit test.cpp to add the code from [https://www.glfw.org/documentation.html](https://www.glfw.org/documentation.html) and compile, link and run the test code getting a black window as expected.

---

<div class="post-metadata">

**Author:** ![pvorisek25](https://avatars.discourse-cdn.com/v4/letter/p/ce7236/32.png) [@pvorisek25](https://discourse.glfw.org/u/pvorisek25)\
**Post date:** [July 27, 2019, 6:04pm UTC](https://discourse.glfw.org/t/compiling-test-program-for-linux-x86/1369/14 "2019-07-27T18:04:32Z")

</div>

Many thanks for very fast response. It works! I also found how to compile GLFW for 32 bit system. Thank you very much.
