gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:54:56 27 4
gpt4 key购买 nike

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

我有 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com