C语言 C - 如何包含保存在单独文件夹中的自己的标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15367855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:40:21  来源:igfitidea点击:

C - how to include own headers kept in separate folder

cheader

提问by Kaiser

First of all I am pretty much a beginner, so I am not sure how to explain what I need but I'll give it a try. (I searched but couldn't find a complete answer).

首先,我几乎是一个初学者,所以我不知道如何解释我需要什么,但我会尝试一下。(我搜索但找不到完整的答案)。

I am learning C on my own and using Code Blocks.

我正在自己学习 C 并使用代码块。

I want to create my own mini-library of custom functions to use in my programs.

我想创建我自己的自定义函数迷你库以在我的程序中使用。

I have a folder called "C". Inside "C", I have a folder called "Exercises" where I do all the little projects from a book.

我有一个名为“C”的文件夹。在“C”中,我有一个名为“Exercises”的文件夹,我可以在其中完成一本书中的所有小项目。

Also inside "C", I wanna have another folder called "MyC" in which I would keep my own header files and the .c files containing the implementations of my custom functions. For example, these .h and .c would be saved in "MyC":

同样在“C”中,我想要另一个名为“MyC”的文件夹,我将在其中保存自己的头文件和包含自定义函数实现的 .c 文件。例如,这些 .h 和 .c 将保存在“MyC”中:

//test.h

//test.h

#ifndef _TEST_H
#define _TEST_H

int mySum(int, int);

#endif // _TEST_H

//test.c

//test.c

#include <stdio.h>
#include "test.h"

int mySum(int a, int b)
{
    return a + b;
}

So now, what I'm trying to do is to be able to create a new project in "Exercises" and not having to bring a copy of both test.h and test.c into the project, but instead just #include my test.h and do something like:

所以现在,我想要做的是能够在“练习”中创建一个新项目,而不必将 test.h 和 test.c 的副本都带入项目中,而只是 #include my test .h 并执行以下操作:

//testMain.c

//testMain.c

#include <stdio.h>
#include <test.h>

int main(void)
{
    printf("\n2 + 1 = %d", mySum(2, 1));

    return 0;
}

I know that <> are for the standard headers, but quotes are for headers in the current folder, and that's what I don't want.

我知道 <> 用于标准标题,但引号用于当前文件夹中的标题,这就是我不想要的。

Is it possible to do this? How?

是否有可能做到这一点?如何?

I've read about going into settings>compiler and on Search Directories add the path where I have the header but didn't work. It gives me an error "undefined reference to 'mySum'" I tried quotes and brackets on the #include.

我已经阅读了有关进入设置>编译器并在搜索目录中添加我有标题但不起作用的路径的信息。它给了我一个错误“对'mySum'的未定义引用”我在#include 上尝试了引号和括号。

Can you guys please give a step-by-step for what it needs to be done to be able to do this?

你们能不能一步一步地说明需要做什么才能做到这一点?

采纳答案by teppic

You've added the header folder, so that's fine. You can #includeas normal. You'd be getting a 'file not found' error if it couldn't find your header.

你已经添加了标题文件夹,所以没问题。你可以#include像往常一样。如果找不到您的标题,您将收到“找不到文件”错误。

But you will also need to link against the object code in this directory, and you'll need to specify which object files to search for your functions. There should be a setting in your IDE to add compiler options, e.g. ../MyC/test.o. If you haven't already compiled the code in these functions, you'll need to specify the .c file instead.

但是您还需要链接到此目录中的目标代码,并且需要指定要搜索您的函数的目标文件。您的 IDE 中应该有一个设置来添加编译器选项,例如../MyC/test.o. 如果您尚未编译这些函数中的代码,则需要改为指定 .c 文件。

回答by pampeho

for headers

用于标题

If you want to move back one folder, do that: #include "../something.h".

如果您想移回一个文件夹,请执行以下操作:#include "../something.h"

In your case, just do this: #include "../MyC/test.h"

在您的情况下,只需执行以下操作: #include "../MyC/test.h"

..simply means go back one directory.

..只是意味着返回一个目录

If you dislike doing it like that, or you want to simply #include "test.h", you can do it using -Icompiler parameter, like this:

如果你不喜欢那样做,或者你想简单地#include "test.h"做,你可以使用-I编译器参数来做,像这样:

-I'../MyC/'

-I'../MyC/'

for c files

对于 c 文件

You need to do a simmiliar thing in compiler parameter.

您需要在编译器参数中做类似的事情。

gcc testMain.c ../MyC/test.c

gcc testMain.c ../MyC/test.c

Just remember that, ..means go back one directory!

请记住,这..意味着返回一个目录

回答by Mosby

When you compile you need to include the -Ioption. For example:

编译时需要包含该-I选项。例如:

gcc -I<path-to-headers> <path-to>/test.c testMain.c -o test_driver

回答by Felipe Lavratti

Considering that "folder_to_code" contains test.hand test.c,

考虑到“folder_to_code”包含test.htest.c

you can add an include folder using the gcccommand:

您可以使用以下gcc命令添加包含文件夹:

gcc -I folder_to_code folder_to_code/test.c testMain.c -Wall -o program.exe

gcc -I folder_to_code folder_to_code/test.c testMain.c -Wall -o program.exe

This way you can add #include "test.h"to the code without problems.

这样您就可以#include "test.h"毫无问题地添加到代码中。