undefined reference to problem in C language

environment

  • ubuntu 18.04 64bit
  • g c c 7.5.0

Introduction

undefined reference to is a classic problem in c/c++ programming, and it is often encountered in actual project development. This article uses some examples to see how similar problems are solved.

Preparation

Prepare 3 files here, the project entry file main.c , the called function is placed in sub.c , and there is also a header file sub.h

sub.h file

 #ifndef __SUB_H__ #define __SUB_H__ void sub(); #endif

sub.c file

 #include <stdio.h> void sub() { printf("This is sub.\n"); }

main.c file

 #include <stdio.h> #include "sub.h" int main() { printf("This is main.\n"); sub(); return 0; }

case one

Let’s look at the first case first. We compile the source code files into object files one by .o

 gcc -c sub.c gcc -c main.c

Next, compile main.o into the executable file main

 gcc -o main main.o

At this time, an error was reported, indicating that the sub method could not be found.

In the face of this situation, adding sub.o and compiling together can solve the problem

 gcc -o main main.o sub.o

Or more directly, a command directly in place

 gcc -o main main.c sub.c

In fact, most projects use Makefile to organize compilation rules, which is very rare.

case two

In project development, it is inevitable to use third-party libraries. At this time, undefined reference to problems are also prone to occur. Let’s take a look

Compile sub.c into a static library

 gcc -c sub.c ar -rc libsub.a sub.o

Next, compile the executable

 gcc -o main main.c

It can be seen that since we have not linked the static library libsub.a , we cannot find the sub method. Here, adding libsub.a can make the problem disappear

 gcc -o main main.c libsub.a

If another static library is called in the called static library, just add it in the same way, and I will not give an example here. It should be noted that the order of library linking is very particular, and undefined reference to will appear if the order is wrong. Libraries that depend on other libraries must be placed in front of the dependent library.

The same applies to dynamic libraries as above.

Generic Makefile template

Here is a basic Makefile template file for your reference

 CC = gcc DEBUG =-DUSE_DEBUG CFLAGS =-Wall SOURCES =$(wildcard ./src/*.c) INCLUDES =-I./include LIB_NAMES = -lz -lm -lrt -ldl -lpthread LIB_PATH =-L./shared_libs OBJ =$(patsubst %.c, %.o, $(SOURCES)) TARGET = main #links $(TARGET):$(OBJ) @mkdir -p build $(CC) $(OBJ) $(LIB_PATH) $(LIB_NAMES) static_libs/libsub.a static_libs/libother.a -o build/$(TARGET) @rm -rf $(OBJ) #compile %.o: %.c $(CC) $(INCLUDES) $(DEBUG) -c $(CFLAGS) $< -o $@ .PHONY:clean clean: @echo "Remove linked and compiled files......" rm -rf $(OBJ) $(TARGET) build

Common Error Solving

 undefined reference to `dlopen' undefined reference to `dlerror' undefined reference to `dlsym' undefined reference to `dlclose'

Workaround, -ldl

 undefined reference to `timer_create' undefined reference to `timer_settime'

Workaround, -lrt

This article is transferred from https://xugaoxiang.com/2023/01/09/c-undefined-reference-to/
This site is only for collection, and the copyright belongs to the original author.