Compiling test program for Linux x86

Hi, again :slight_smile:,

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

What I am doing wrong?

Thanks for answers

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

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 and then compiling with the library as per the documentation on building applications.

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?

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.

1 Like

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?

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.

Thanks for response. I have one question about generating GLFW. In this section (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) 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.

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.

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 (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:

cd ~/Projects/glfw_build
cmake ../glfw

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

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

I then build the code using make:

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:

./examples/boing

You should see a spinning bouncing ball.

1 Like

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.

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

-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.

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.

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:

#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:

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 and compile, link and run the test code getting a black window as expected.

2 Likes

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