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

标签 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

#define MAX_LENGTH 21.8
#define WORK_WEEK  5

int main(void) {
    function1();
    return EXIT_SUCCESS;
}

myLibrary.c

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

void function2(void){
    //This function does nothing as well
}

myLibrary.h

#ifndef MYLIBRARY_H_
#define MYLIBRARY_H_

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

#endif /* MYLIBRARY_H_ */

首先,我导航到我的工作目录。 通常在没有本地 header 的文件中我会输入:

gcc -o 02_01 02_01.c
./02_01

它会起作用。

我尝试过多种方法,例如:

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

这给了我一个错误“函数‘puts’的隐式声明

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

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

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

最佳答案

您必须在使用包含函数的每个文件中包含必需的 header 。在您的情况下,您必须包括 #include <stdio.h>在你的 myLibrary.c 的开头文件。

此外,您可能想构建 .a库并稍后链接。

所以,最后:

  1. 编译lib:

         gcc -c -o mylib myLibrary.c
    
  2. 制作静态库:

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

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

关于ubuntu下编译头文件。我在终端中输入什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46404965/

相关文章:

c - 为什么指向 NULL 的 init 指针是一个好习惯?

gcc - CMake 相当于 -l 作为 ld 链接到库的 gcc 指令

c - 检查目录时出现指针问题

gcc - gcc 使用哪个链接器?

c - 将数据存储在列表 c 中

c - 动态数组堆栈结构 C

c - OpenBSD 上原始套接字 icmp 的协议(protocol)族不支持 sendto 地址族

linux - 杀死进程和所有后代的安全方法

linux - ubuntu安装gitlab失败

c++ - CGAL新手---入门难点: CGAL_Qt5 Returns FALSE in CMake