Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 在ubuntu中编译头文件。在终端中输入什么?_C_Ubuntu_Gcc_Terminal - Fatal编程技术网

C 在ubuntu中编译头文件。在终端中输入什么?

C 在ubuntu中编译头文件。在终端中输入什么?,c,ubuntu,gcc,terminal,C,Ubuntu,Gcc,Terminal,我很确定这是一个简单的问题,但我已经在网上搜索了大约半个小时 我有3个文件: 02_01.c #include <stdio.h> // Notice the library included in the header of this file #include <stdlib.h> #include "myLibrary.h" // Notice that myLibrary.h uses different include syntax

我很确定这是一个简单的问题,但我已经在网上搜索了大约半个小时

我有3个文件:

02_01.c

#include <stdio.h>          // Notice the library included in the header of this file
#include <stdlib.h>

#include "myLibrary.h"      // Notice that myLibrary.h uses different include syntax

#define MAX_LENGTH 21.8
#define WORK_WEEK  5

int main(void) {
    function1();
    return EXIT_SUCCESS;
}
我的图书馆

#ifndef MYLIBRARY_H_
#define MYLIBRARY_H_

void function1(void);
void function2(void);

#endif /* MYLIBRARY_H_ */
首先,我导航到我的工作目录。 通常在没有本地头的文件中,我会键入:

gcc -o 02_01 02_01.c
./02_01
这会奏效的

我试过很多方法,比如:

gcc-o 02_01 02_01.c myLibrary.c

void function1(void){
    puts("It works :)");
}

void function2(void){
    //This function does nothing as well
}
这给了我一个函数“puts”的错误隐式声明

gcc-o myLibrary myLibrary.c也给出了相同的错误

我应该在ubuntu的终端中输入什么


因此,我假设myLibrary.c中的put函数未连接到02_01.c,这是我包含stdio.h的地方。

您必须在使用包含函数的每个文件中包含所需的头。在您的情况下,必须在myLibrary.c文件的开头包含

此外,您可能希望构建一个库,并在以后与之链接

因此,最后:

编译库:

     gcc -c -o mylib myLibrary.c
生成静态库:

     ar rcs libMyLib.a mylib
编译应用程序并链接到一起:

     gcc -o 02_01 02_01.c -L. -lMyLib

您必须包括使用函数的头,而不是在另一个.c文件中。gcc无法在两个文件之间建立链接。你的问题是什么?您不想更改文件的内容吗?myLibrary.c中的代码使用中声明的标准I/O函数。那么,也许您应该在myLibrary.c中包含该头文件?头文件只是当前正在编译的文件的一部分,它们不会溢出到其他独立的翻译单元。翻译单元基本上是一个源文件及其包含的所有头文件,因此是一个独立的单元。@Jean-Françoisfab谢谢。这就是我在C++中所做的。我实际上是在听林达的视频课,这些是作为例子提供的文件。他们在Windows中使用Eclipse,所以也许这就是为什么它在Ubuntu上对他们有效,而对我无效的原因。@Someprogrammerdude谢谢,我就是这么做的,它工作得很好。你了解了,你写了一个好的make文件,然后你只需键入make