源文件找不到头文件 [英] Source file cannot find the header files

查看:243
本文介绍了源文件找不到头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过这些链接:<一个href=\"http://stackoverflow.com/questions/3667184/header-files-linked-to-from-header-file-not-found?lq=1\">This 之一

和其他几个类似的的。
 这里给出的答案都不是工作方法的工作。

我有两个源文件 a1.c a2.c 和两个头文件 a1.h a2.h 。我想在这两个文件中的头文件(在a1.c和a2.c因为有我需要从a2.c使用功能)

我已经包括

 的#includea1.h
#包括a2.h

在a1.c源文件

我使用的是Ubuntu的GCC。并且使用命令的gcc -o a1.h -lm a1.out
而没有工作。

我试着用

 的gcc -c -I /主页/文档/ ctests / a1.c -o a1.out

以及

  GCC -c a1.c -I /主页/文档/ ctests / -o a1.out

我的拼写都还好,以及(有几乎没有任何闪失有一个字母和数字作为文件名反正)。

此外,所有的文件都在同一个文件夹中。

我知道这可能是一个微不足道的问题,但我被困在这一个和将AP preciate任何帮助。我是比较新的编程和完全新的Linux和Unix尽量使用命令行去。

非常感谢!


解决方案

  GCC -c

告诉GCC编译器的文件对象(你看到到处.o文件)。要与其他一些.o文件为可执行后来联系。

所以,你想要做的是无论是单独编译两个文件,​​后来将它们链接。是这样的。

 的gcc -I/主页/文档/ ctests /-c a1.c
GCC -I/主页/文档/ ctests /-c a2.cGCC -o myprogram a1.o a2.o

或者只是编译和链接在同一时间。

 的gcc -I/主页/文档/ ctests /a2.c a1.c -o myprogram

然后运行您的程序像

  path_to / myprogram

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)

I have included

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

in the source files of a1.c

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

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.

Many thanks!

解决方案

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.

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 

这篇关于源文件找不到头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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