GCC:如何使该编译和链接工作? [英] GCC: How can I make this compiling and linking work?

查看:99
本文介绍了GCC:如何使该编译和链接工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用文件tester-1.c中在libdrm.h中定义并在libdrm.c中实现的函数.这三个文件位于同一文件夹中,并使用pthread函数.

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

它们的包含文件为:

libdrm.h

#ifndef __LIBDRM_H__
#define __LIBDRM_H__

#include <pthread.h>

#endif

libdrm.c<-没有main()

libdrm.c <- has no main()

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

tester-1.c<-具有main()

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

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

libdrm.c的编译器错误说:

The 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

tester-1.c的编译器错误说:

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'

所有这些功能都在libdrm.c中定义

All these functions are defined in libdrm.c

我应该使用哪些gcc命令来编译和链接这些文件?

What gcc commands should I use to make these files compile and link?

推荐答案

要将您的.c源代码编译为目标文件,请使用GCC的-c选项.然后,您可以针对所需的库将目标文件链接到可执行文件:

To compile your .c sources to object files, use GCC's -c option. 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不能产生可执行文件,因为它没有main()函数.
  • tester-1.c的链接失败,因为没有向链接程序通知libdrm.c中定义的必需符号.
  • libdrm.c alone couldn't produce an executable, because it does not have a main() function.
  • tester-1.c's linking failed, because the linker wasn't informed about the required symbols defined in libdrm.c.

使用-c选项,GCC可以编译和汇编源代码,但是会跳过链接,而留下 .o 文件,可以链接到可执行文件或打包到库中.

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.

这篇关于GCC:如何使该编译和链接工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆