C Compilation error in Ubuntu 13.10, probably error in makefile - c

I starting an project using OpenGL in Linux (Ubuntu 13.10) and i have an basic source code from my professor that compiles in his computer.
When i treid to compile it here, (i have the open gl libraries working in other projects), i got the following error in terminal:
gcc -o quadtree quadtree.o glm.o winGL.o -lglut -lGL -lGLU
/usr/bin/ld: glm.o: referência indefinida ao símbolo 'acos##GLIBC_2.2.5'
/lib/x86_64-linux-gnu/libm.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make: ** [quadtree] Erro 1
("referência indefinida ao simbolo" means "undefined reference to symbol")
I searched in internet and got hints that this could be an error in the makefile, but i dont know what is wrong. There is my makefile:
.c.o: $*.h
gcc -c $*.c
.cpp.o: $*.h
g++ -c $*.cpp
all: quadtree
quadtree: quadtree.o glm.o winGL.o
gcc -o $# $^ -lglut -lGL -lGLU
clean:
rm *.o *.*~ *~ quadtree
Is there any command missing?

You forgot to link with -lm, see man acos

Related

C Makefile - Cannot find library -lrssnews

I have reviewed the answer provided Here but I still cannot seem to make progress.
I am attempting to follow along with the online Stanford course CS107. I was able to complete assignments 2-3 but am stuck on assignment number 4.
I receive this error when I type "make" in the command line.
gcc -g -Wall -std=gnu99 -Wno-unused-function -c -o rss-news-search.o rss-news-search.c
gcc rss-news-search.o -g -Wall -std=gnu99 -Wno-unused-function -g -lnsl -lrssnews -L/home/pi/Desktop/C/StanfordHW/Assn4/assn-4-rss-news-search-lib/ -o rss-news-search
/usr/bin/ld: cannot find -lrssnews
collect2: error: ld returned 1 exit status
make: *** [Makefile:32: rss-news-search] Error 1
Here is the snippet of my Makefile:
CFLAGS = -g -Wall -std=gnu99 -Wno-unused-function $(DFLAG)
LDFLAGS = -g $(SOCKETLIB) -lnsl -lrssnews -L/home/pi/Desktop
/C/StanfordHW/Assn4/assn-4-rss-news-search-lib/$(OSTYPE)
PFLAGS= -linker=/usr/pubsw/bin/ld -best-effort
So, I tried doing what the previous answer suggested by moving the file "librssnews.a" to the folder /lib from where I am executing the make command. But this did not work.
Here is an image of my directory
Is there something else that I can try? Please excuse any obvious mistakes, as I'm still learning. Thank you.

C Makefile Linking Error: Undefined reference to 'main'

I have tried looking through other answers, unfortunately I have not found anyone with quite the same problem. I am trying to link two C files together, one has a header file, and the other the main file. When trying to make I receive this error:
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
Below is the makefile I use to compile.
INC_DIR = include
SRC_DIR = src
BIN_DIR = bin
UNAME := $(shell uname)
CC = gcc
CFLAGS = -Wall -std=c11 -g -I$(INC_DIR) -I/usr/include/libxml2/
LDFLAGS= -L.
all: XMLParser
XMLParser: $(BIN_DIR)/XMLParser.o $(BIN_DIR)/SVGParser.o
$(CC) $(CFLAGS) -o XMLParser $(BIN_DIR)/XMLParser.o $(BIN_DIR)/SVGParser.o
$(BIN_DIR)/XMLParser.o: $(SRC_DIR)/XMLParser.c $(INC_DIR)/SVGParser.h
$(CC) $(CFLAGS) -o $(BIN_DIR)/XMLParser.o -c $(SRC_DIR)/XMLParser.c
$(BIN_DIR)/SVGParser.o: $(SRC_DIR)/SVGParser.c $(INC_DIR)/SVGParser.h
$(CC) $(CFLAGS) `xml2-config --cflags --libs` -o $(BIN_DIR)/SVGParser.o $(SRC_DIR)/SVGParser.c
clean:
rm *.o XMLParser $(BIN_DIR)/*
XMLParser.c has my main function:
int main(int argc, char **argv) {
...;
return 0;
}
Your recipe for generating the intermediate SVGParser.o is attempting to link and generate a runnable executable. You need to add -c to skip the link step.
You will also need to remove the —libs on that object file and move it to the final link step. In other words add xml2-config —libs for the rule generating the final XMLParser target.
Disclaimer: I’ve never specifically used xml2-config, so I guess there’s some chance I’m wrong here, but it’s a very common paradigm for building and linking against complex libraries, I assume it’s following the same patterns.
A full sample code showing the error must be posted to be able to test it elsewhere, so I cannot fully test your makefile, but providing a true main() and see what happens.
After generating stub files to check your Makefile I found the following output from make:
$ make
gcc -Wall -std=c11 -g -Iinclude -I/usr/include/libxml2/ -o bin/XMLParser.o -c src/XMLParser.c
gcc -Wall -std=c11 -g -Iinclude -I/usr/include/libxml2/ `xml2-config --cflags --libs` -o bin/SVGParser.o src/SVGParser.c
/usr/local/bin/ld: /usr/lib/gcc/x86_64-unknown-freebsd13.0/10.2.1/../../../crt1.o: en la función `_start':
/home/usr/src/lib/csu/amd64/crt1_c.c:75: referencia a `main' sin definir
collect2: error: ld devolvió el estado de salida 1
*** Error code 1
Stop.
make: stopped in /home/lcu/so/makefile
$ _
which is trying to link (alone) the module SVGParser.o (THIS IS BECAUSE YOU DIDN'T ADD THE -c COMPILATION OPTION TO COMPILE ONLY).
Change the compilation line for the SVGParser.o module to use the -c compile-only option and you will get the executable properly.

C how do i import/link glfw with gcc on linux

this shit doesn't work. why?
gcc -c -Wall src/*.c -g -m64 && gcc -Iinclude *.o -o bin/debug/main -Lusr/local/lib -l libglfw && ./bin/debug/main
i get this linking error when trying to build the glfw window example
/usr/bin/ld: cannot find -l libglfw
collect2: error: ld returned 1 exit status
How do i import/link glfw and what is going wrong? I tried dynamically linking from usr/lib/x86_64-linux-gnu/libglfw.so but it didn't help. I'm pretty new to gcc and linux so i could just be stupid.
EDIT:
the comments pointed out a few mistakes i made so here's the new command, which is wrong too for some reason:
gcc -c -Wall src/*.c -g -m64 && gcc -Iinclude *.o -o bin/debug/main -L/usr/local/lib -lglfw && ./bin/debug/main
the new error:
/usr/bin/ld: main.o: in function `main':
/home/basti/dev/OpenGL/src/main.c:26: undefined reference to `glClear'
collect2: error: ld returned 1 exit status
I figured it out. Thanks to comments i managed to fix linking and didn't realize i had to import other opengl stuff.
In case somone is searching for an answer to the same problem here's my debug build command for sublime text 3:
gcc -c -Wall src/*.c -g -m64 && gcc -Iinclude *.o -o bin/debug/main -lglfw -lGLU -lGL && ./bin/debug/main

Mac OS gcc linking c and asm

I decided to learn how to make own OS by tutorial, I wrote boot on NASM and kernel on C also linker.ld and makefile. When I try linking all together I have this kind of error:
ld: library not found for -lgcc
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Also, I have installed binutils using homebrew but it doesn't help me. I have Mac OS 10.14
And this is my makefile:
CC = gcc
main: kernel.c linker.ld boot.asm
nasm -felf32 boot.asm -o boot.o
$(CC) -c kernel.c -o kernel.o -std=gnu99 -ffreestanding -Wall -Wextra
$(CC) -T linker.ld -o basilica.bin -ffreestanding -nostdlib boot.o kernel.o -lgcc
cp basilica.bin isodir/boot/basilica.bin
grub-mkrescue -o basilica.iso isodir
clean:
rm ./*.o ./*.bin ./*.iso ./isodir/boot/*.bin
What am I doing wrong? How can I fix this error: ld: library not found for -lgcc? Please help me.

How do I change my makefile to avoid the undefined reference to a function in the maths library?

I'm trying to install PintOS on my local Ubuntu 14.04 machine. When I try to run make to compile the utilities. I get the following error.
ankitkal#ankitkal-Inspiron-5521:~/os/pintos/src/utils$ ls
backtrace Makefile pintos pintos.~1.55.~ pintos-mkdisk setitimer-helper.o squish-unix.c
CVS Makefile~ pintos~ pintos-gdb setitimer-helper.c squish-pty.c
ankitkal#ankitkal-Inspiron-5521:~/os/pintos/src/utils$ make
gcc -lm setitimer-helper.o -o setitimer-helper
setitimer-helper.o: In function `main':
setitimer-helper.c:(.text+0xbe): undefined reference to `floor'
collect2: error: ld returned 1 exit status
make: *** [setitimer-helper] Error 1
ankitkal#ankitkal-Inspiron-5521:~/os/pintos/src/utils$
The maths library (for the <math.h> header which is used in setitimer-helper.c) is not getting linked properly. When I look into the Makefile, this is the output.
ankitkal#ankitkal-Inspiron-5521:~/os/pintos/src/utils$ cat Makefile
all: setitimer-helper squish-pty squish-unix
CC = gcc
CFLAGS = -Wall -W
LDFLAGS = -lm
setitimer-helper: setitimer-helper.o
squish-pty: squish-pty.o
squish-unix: squish-unix.o
clean:
rm -f *.o setitimer-helper squish-pty squish-unix
Please tell me how to fix it. I'm using gcc-4.8.6 by the way.
gcc -lm setitimer-helper.o -o setitimer-helper
The problem is in the order of your arguments to GCC. Try this:
gcc -o setitimer-helper setitimer-helper.o -lm
This is because of the way that ld resolves undefined symbols when linking. Basically, the way you had it before, ld first sees -lm and says "I have no reason to include this library". It then includes your setitimer-helper.o which has an unresolved reference to floor. After that, there are no more libraries to consider, and floor remains unresolved.
If -lm comes afterward, it is able to resolve the reference to floor.

Resources