包括C中的外部库 [英] Include an external library in C

查看:115
本文介绍了包括C中的外部库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用C库哈佛大学的开放式课件课程。教师的设立外部LIB的指令可以发现 rel=\"nofollow\">。

I'm attempting to use a C library for an opencourseware course from Harvard. The instructor's instructions for setting up the external lib can be found here.

我下面具体到Ubuntu的说明,因为我想用这个LIB在我的Ubuntu框。我跟着页面上的说明来进行设置,但是当我运行使用CS50库函数的简单 helloWorld.c 程序,GCC并不想一起玩。

I am following the instructions specific to ubuntu as I am trying to use this lib on my ubuntu box. I followed the instructions on the page to set it up, but when I run a simple helloWorld.c program using a cs50 library function, gcc doesn't want to play along.

例如:

helloWorld.c

#include <stdio.h>
#include <cs50.h>

int
main(void){
        printf("What do you want to say to the world?\n");
        string message = GetString();
        printf("%s!\n\n", message);
}

$ GCC helloWorld.c

/tmp/ccYilBgA.o: In function `main':
helloWorld.c:(.text+0x16): undefined reference to `GetString'
collect2: ld returned 1 exit status

我也跟着信中的说明如在说明书中提到的,但他们没有为我工作。我乳宁的Ubuntu 12.04。请让我知道如果我能进一步澄清我的问题。

I followed the instructions to the letter as stated in the instructions, but they didn't work for me. I'm runing ubuntu 12.04. Please let me know if I can clarify further my problem.

推荐答案

首先,作为一个初学者,你应该总是问GCC编译所有的警告和启用调试信息,即的gcc -Wall -g

First, as a beginner, you should always ask GCC to compile with all warnings and debugging information enabled, i.e. gcc -Wall -g

那么你可能错过了一些具体的哈佛大学图书馆,有些选项,如 -L 其次是图书馆的目录,那么 -l <​​/ code>粘在库名。所以,你可能需要的gcc -Wall -g -lcs50 (更换 CS50 通过适当的名称),你可能需要一些 -L 一些-DIR

Then you are probably missing some Harvard specific library, some options like -L followed by a library directory, then -l glued to the library name. So you might need gcc -Wall -g -lcs50 (replace cs50 by the appropriate name) and you might need some -Lsome-dir

注意,程序参数 GCC 的顺序是显著。作为一般规则,如果 A 取决于 B 你应该把 A b ;更具体的建议。

Notice that the order of program arguments to gcc is significant. As a general rule, if a depends upon b you should put a before b; more specifically I suggest


  1. 开始使用 GCC 节目名称;添加的 C 的标准水平如 -std = C99 如果想

  2. 把编译器警告,调试(或优化)选项,如: -Wall -g (你甚至可能要添加 -Wextra 来获得更多的警告)。

  3. 把preprocessor的定义,包括例如目录 -DONE = 1 -Imy-包括-DIR /

  4. 把你的 C 的源文件的hello.c

  5. 将与您链接的目标文件,即文件bar.o

  6. 把库目录 -Lmy-LIB-DIR / 如果相关

  7. 陈建库名称 -laa -lbb (当 libaa.so 取决于 libbb.so ,按照这个顺序)

  8. 结束与 -o你的程序名给生产二进制文件的名称。不要使用默认名称的a.out

  1. Start with the gcc program name; add the C standard level eg -std=c99 if wanted
  2. Put compiler warning, debugging (or optimizing) options, eg -Wall -g (you may even want to add -Wextra to get even more warnings).
  3. Put the preprocessor's defines and include directory e.g. -DONE=1 and -Imy-include-dir/
  4. Put your C source file hello.c
  5. Put any object files with which you are linking i.e. bar.o
  6. Put the library directories -Lmy-lib-dir/ if relevant
  7. Pur the library names -laa and -lbb (when the libaa.so depends upon libbb.so, in that order)
  8. End with -o your-program-name to give the name of the produced binary. Don't use the default name a.out

目录给选项 -I (为preprocessor包含)和 -L 图书馆可以给出好几次,顺序是显著(搜索顺序)。

Directory giving options -I (for preprocessor includes) and -L for libraries can be given several times, order is significant (search order).

很快,你会想用建设者的工具,如制作

Very quickly you'll want to use builder tools like make

也了解如何使用调试器 GDB

Learn also to use the debugger gdb

获取习惯,总是要求从编译器警告,始终把提高你的程序,直到你得到任何警告:编译器是你的朋友,它是帮助您

Get the habit to always ask for warnings from the compiler, and always improve your program till you get no warnings: the compiler is your friend, it is helping you!

玩得开心。

这篇关于包括C中的外部库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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