源文件无法找到头文件

时间:2022-05-10 15:06:16

I have looked at these links : This one and This and a couple of other similar ones. None of the answers given here are working methods are working.

我看过这些链接:这个,这个,还有其他几个类似的。这里给出的答案都不是有效的方法。

I have a two source files a1.c , a2.c and two header files a1.h and a2.h . I want to include the header files in both these files (and a2.c in a1.c as there is a function I need to use from a2.c)

我有两个源文件a1。c,a2。c和两个头文件a1。h和a2。h。我想在这两个文件(和a2中包含头文件。c在a1。c因为我需要从a2.c中使用一个函数)

I have included

我已经包括

#include "a1.h"  
#include "a2.h"

in the source files of a1.c

在a1.c的源文件中。

I'm using GCC on Ubuntu. and using the command gcc a1.h -o a1.out -lm and that didn't work.

我在Ubuntu上使用GCC。使用gcc a1命令。h - o a1。我出了门,但那不管用。

I tried with

我试着用

gcc -c -I/Home/Documents/ctests/ a1.c -o a1.out

as well as

以及

gcc -c a1.c -I/Home/Documents/ctests/ -o a1.out

My spellings are okay as well (there's hardly any room for error there with one letter and a number as the filename anyway).

我的拼写也很好(这里几乎没有错误的空间,只有一个字母和一个数字作为文件名)。

Also, all the files are in the same folder.

而且,所有的文件都在同一个文件夹中。

I know this may be a trivial question but I am stuck on this one and would appreciate any help. I am relatively new to programming and completely new to Linux and Unix as far as using the command line goes.

我知道这可能是一个微不足道的问题但是我困在这个和任何帮助,不胜感激。我是相对较新的编程和全新的Linux和Unix使用命令行去。

Many thanks!

很多谢谢!

3 个解决方案

#1


3  

gcc -c

tells gcc to compile the file to object (the .o files you see everywhere). To be linked later with some other .o files to an executable.

告诉gcc将文件编译为object(您随处可见的.o文件)。以后与其他.o文件链接到可执行文件。

So what you want to do is either compile the two files separately and link them later. like this.

所以你要做的是分别编译这两个文件并在以后链接它们。像这样。

gcc -I"/Home/Documents/ctests/" -c a1.c
gcc -I"/Home/Documents/ctests/" -c a2.c

gcc -o myprogram a1.o a2.o 

Or just compile and link at the same time.

或者同时编译和链接。

gcc -I"/Home/Documents/ctests/" a2.c a1.c -o myprogram

And then run your program like

然后像这样运行你的程序

path_to/myprogram 

#2


1  

Compile everything, and link it together.

编译所有内容,并将其链接在一起。

If all files are in one directory, this should work:

如果所有文件都在一个目录中,这应该可以工作:

  gcc a1.c a2.c -o myapp

When you want to create separate object files, do this:

当您想要创建单独的对象文件时,请这样做:

  gcc -c a1.c a2.c

Then you can then link together to create an application:

然后您可以链接在一起创建一个应用程序:

  gcc a1.o a2.o -o myapp

#3


0  

Your gcc command should be like this

您的gcc命令应该是这样的

gcc -I/Home/Documents/ctests/ -o a1.out a1.c

and you have to include a1.h and a2.h header file in your a1.c like this

必须包含a1。h和a2。h头文件在a1中。c这样的

#include "a1.h"
#include "a2.h"

If you are calling some function from a2.c in your a1.c then you have to build your program in this way

如果你从a2调用某个函数。c在a1。然后你必须用这种方式构建你的程序

gcc -I/Home/Documents/ctests/ -o a1.out a2.c a1.c

#1


3  

gcc -c

tells gcc to compile the file to object (the .o files you see everywhere). To be linked later with some other .o files to an executable.

告诉gcc将文件编译为object(您随处可见的.o文件)。以后与其他.o文件链接到可执行文件。

So what you want to do is either compile the two files separately and link them later. like this.

所以你要做的是分别编译这两个文件并在以后链接它们。像这样。

gcc -I"/Home/Documents/ctests/" -c a1.c
gcc -I"/Home/Documents/ctests/" -c a2.c

gcc -o myprogram a1.o a2.o 

Or just compile and link at the same time.

或者同时编译和链接。

gcc -I"/Home/Documents/ctests/" a2.c a1.c -o myprogram

And then run your program like

然后像这样运行你的程序

path_to/myprogram 

#2


1  

Compile everything, and link it together.

编译所有内容,并将其链接在一起。

If all files are in one directory, this should work:

如果所有文件都在一个目录中,这应该可以工作:

  gcc a1.c a2.c -o myapp

When you want to create separate object files, do this:

当您想要创建单独的对象文件时,请这样做:

  gcc -c a1.c a2.c

Then you can then link together to create an application:

然后您可以链接在一起创建一个应用程序:

  gcc a1.o a2.o -o myapp

#3


0  

Your gcc command should be like this

您的gcc命令应该是这样的

gcc -I/Home/Documents/ctests/ -o a1.out a1.c

and you have to include a1.h and a2.h header file in your a1.c like this

必须包含a1。h和a2。h头文件在a1中。c这样的

#include "a1.h"
#include "a2.h"

If you are calling some function from a2.c in your a1.c then you have to build your program in this way

如果你从a2调用某个函数。c在a1。然后你必须用这种方式构建你的程序

gcc -I/Home/Documents/ctests/ -o a1.out a2.c a1.c