GCC: How can I do this compilation and binding work?

I want to use the functions tester-1.c from the files that I defined in libdrm.h and gave an implementation in libdrm.c. These three files are in the same folder and use the pthread functions.

Their included files:

libdrm.h

#ifndef __LIBDRM_H__
#define __LIBDRM_H__

#include <pthread.h>

#endif

libdrm.c <- does not have the main ()

#include <stdio.h>
#include <pthread.h>
#include "libdrm.h"

tester-1.c <- has the main ()

#include <stdio.h>
#include <pthread.h>
#include "libdrm.h"

A compiler error for libdrm.c says:

gcc libdrm.c -o libdrm -l pthread
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status

And the compiler errors for tester-1.c say:

gcc tester-1.c -o tester1 -l pthread
/tmp/ccMD91zU.o: In function `thread_1':
tester-1.c:(.text+0x12): undefined reference to `drm_lock'
tester-1.c:(.text+0x2b): undefined reference to `drm_lock'
tester-1.c:(.text+0x35): undefined reference to `drm_unlock'
tester-1.c:(.text+0x3f): undefined reference to `drm_unlock'
/tmp/ccMD91zU.o: In function `thread_2':
tester-1.c:(.text+0x57): undefined reference to `drm_lock'
tester-1.c:(.text+0x70): undefined reference to `drm_lock'
tester-1.c:(.text+0x7a): undefined reference to `drm_unlock'
tester-1.c:(.text+0x84): undefined reference to `drm_unlock'
/tmp/ccMD91zU.o: In function `main':
tester-1.c:(.text+0x98): undefined reference to `drm_setmode'
tester-1.c:(.text+0xa2): undefined reference to `drm_init'
tester-1.c:(.text+0xac): undefined reference to `drm_init'
tester-1.c:(.text+0x10e): undefined reference to `drm_destroy'
tester-1.c:(.text+0x118): undefined reference to `drm_destroy'

All these functions are defined in libdrm.c

What gcc commands should be used to compile and link these files?

+3
source share
3 answers

.c , GCC -c. :

gcc libdrm.c -c
gcc tester-1.c -c
gcc tester-1.o libdrm.o -o tester1 -lpthread

, , . , .

, (= ) .

  • libdrm.c , main().
  • tester-1.c , , libdrm.c.

-c GCC , , .o , .

+11
gcc tester-1.c libdrm.c -o tester1 -l pthread

, . , libdrm.c , tester1.c .

+1
gcc test-1.c libdrm.c -o libdrm -l pthread
+1
source

Source: https://habr.com/ru/post/1789806/


All Articles