简体   繁体   English

GCC:我如何使这个编译和链接工作?

[英]GCC: How can I make this compiling and linking work?

I want to use from the file tester-1.c functions that I defined in libdrm.h and gave implementation in libdrm.c. 我想使用我在libdrm.h中定义的文件tester-1.c函数,并在libdrm.c中给出了实现。 The three files are on the same folder and use pthread functions. 这三个文件位于同一文件夹中并使用pthread函数。

Their include files are: 他们的包含文件是:

libdrm.h libdrm.h

#ifndef __LIBDRM_H__
#define __LIBDRM_H__

#include <pthread.h>

#endif

libdrm.c <- has no main() libdrm.c < - 没有main()

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

tester-1.c <- has teh main() tester-1.c < - 有teh main()

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

The compiler error for libdrm.c says: libdrm.c的编译器错误说:

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: 而且test-1.c的编译器错误说:

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 所有这些函数都在libdrm.c中定义

What gcc commands should I use to make these files compile and link? 我应该用什么gcc命令来编译和链接这些文件?

To compile your .c sources to object files , use GCC's -c option. 要将.c源代码编译为目标文件 ,请使用GCC的-c选项。 Then you can link the object files to an executable, against the required library: 然后,您可以针对所需的库将目标文件链接到可执行文件:

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

Doing compiling and linking in one go, as many others have suggested, works fine too. 正如许多其他人所建议的那样,一次性编译链接也可以正常工作。 However, it's good to understand that the build process involves both of these stages. 但是,理解构建过程涉及这两个阶段是很好的。

Your build failed, because your translation modules (=source files) required symbols from each other. 您的构建失败,因为您的翻译模块(=源文件)需要彼此符号。

  • libdrm.c alone couldn't produce an executable, because it does not have a main() function. 单独的libdrm.c无法生成可执行文件,因为它没有main()函数。
  • tester-1.c 's linking failed, because the linker wasn't informed about the required symbols defined in libdrm.c . tester-1.c的链接失败,因为链接器没有被告知libdrm.c定义的必需符号。

With -c option, GCC compiles and assembles the source, but skips linking, leaving you with .o files, which can be linked into an executable or packaged into a library. 使用-c选项,GCC编译和汇编源,但跳过链接,留下.o文件,可以链接到可执行文件或打包到库中。

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

You need to compile all the source files in one go rather than doing them individually. 您需要一次编译所有源文件,而不是单独编译它们。 Either that or compile libdrm.c as a library and then link it with tester1.c when you compile that. 或者将libdrm.c编译为库,然后在编译时将其与tester1.c链接。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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