简体   繁体   中英

Can't link opengl libraries in makefile

I am writing a hybrid program in Cpp + x86 assembly. The project consists of 3 files: main.cc, data.cc and a_t.asm. I'm stuck with a makefile (in which I am a total beginner) part. It looks like this:

CC=gcc
ASMBIN=nasm
CFLAGS=-m32 -Wall

all: main.o a_t.o
    $(CC) -m32 main.o a_t.o -lGL -lGLU -lglut 

a_t.o:
    $(ASMBIN) -f elf a_t.asm

main.o: main.cc data.cc
    $(CC) $(CFLAGS) -g main.cc

clean:
    rm -rf *o main

upon make command I get the following:

gcc -m32 main.o a_t.o -lGL -lGLU -lglut 
/usr/bin/ld: cannot find -lGL
/usr/bin/ld: cannot find -lGLU
/usr/bin/ld: cannot find -lglut
collect2: error: ld returned 1 exit status
make: *** [all] Error 1

I've tried putting "-lGL -lGLU -lglut" part in different orders, but nothing have worked. I have all three libraries installed and working. I would appreciate your help.

So the problem was that using -m32 flag requires 32 bit libraries. As both people, to whom I'm extremely grateful, who answered me suggested, I should have installed the libraries, only the 32 bit versions of them.

libglu1-mesa-dev:i386
libgl1-mesa-dev:i386
freeglut3-dev:i386

For those who can use this answer, keep in mind that the install of i386 package, removes amd64, and vice versa. So don't forget to reinstall that packages with a :amd64 specifier.

Makefiles can be a bit arcane, somewhat fussy to get right, and there's a lot of voodoo floating around about how to write them. Here is a more reasonable, fixed-up makefile:

# Don't set CC=gcc, because it's not 1995 any more.
ASMBIN = nasm
# CFLAGS is for C, CXXFLAGS is for C++
# Also, let's put -g here
CXXFLAGS = -m32 -Wall -g
LDFLAGS = -m32

# Use pkg-config wherever possible
opengl_libs := -lglut $(shell pkg-config --libs gl glu)
opengl_cflags := $(shell pkg-config --cflags gl glu)

all: main

main: main.o a_t.o
    # Order of flags is important here!
    # We also have to use CXX instead of CC to avoid linker errors.
    $(CXX) $(LDFLAGS) -o $@ $^ $(opengl_libs)

a_t.o:
    $(ASMBIN) -f elf a_t.asm

main.o: main.cc data.cc
    # Don't forget -c and -o
    $(CXX) $(CXXFLAGS) $(opengl_cflags) -c $< -o $@

clean:
    # Should be *.o, not *o
    rm -rf *.o main

.PHONY: all clean

However, this probably won't fix the error you are running into. You need to remember to install the development version of the OpenGL libraries. On Debian-based systems, this would mean installing the following packages (for i386, of course):

  • libglu1-mesa-dev
  • libgl1-mesa-dev
  • freeglut3-dev .

The pkg-config program is probably already installed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM