为什么我的 include 指令不能在 gcc 中工作?
我正在编写一些使用开放动态引擎的代码。我以前在 Windows 上使用过这段代码,但现在我要转向 unix,这样我就可以获得在 unix 环境下使用 C 的经验,所以我不依赖 Visual Studio。我将所有内容下载到我的主目录,使用包含的 makefile 构建并尝试了演示;一切都很好。我用自己的测试创建了一个目录和一个测试文件。对于我的#include,我指定:
#include ".././ode-0.11.1/include/ode/ode.h"
#include ".././ode-0.11.1/include/drawstuff/drawstuff.h"
因为该库仅位于我的主目录中,而不是位于标准位置。我去编译我的代码,但是:
.././ode-0.11.1/include/ode/ode.h:28:27: fatal error: ode/odeconfig.h: No such
file or directory
查看 ode.h,它包含一堆其他标头,所有标头都位于同一目录中,但使用括号语法而不是引号。我猜这与 gcc 无法找到其他标头的原因有关。我已经查看了 makefile,但还不够了解我的问题是什么。为什么我的编译不起作用?详细/彻底的答案将不胜感激,因为我想了解这一切是如何工作的(链接、包含、制作等)。
编辑:所以我弄清楚了如何正确包含标题,现在我需要弄清楚如何链接到函数的库定义...
Edit2:仍然无法弄清楚如何链接到我的代码。编译后的静态库转储到绘图函数的“~/ode-0.11.1/ode/src/.libs”和“~/ode-0.11.1/drawstuff/src/.libs”中。
编辑3:我想我明白了。我没有正确使用 -l 选项,它似乎必须位于引用我链接到的库中的函数的文件之后。
I'm working on some code that uses Open Dynamics Engine. I've worked with this code before on windows, but now I'm moving over to unix so I can get experience working with C under a unix environment and so I'm not reliant on visual studio. I downloaded everything to my home directory, built using the included makefile and tried a demo; everything's good. I made a directory and a test file in it with my own test. For my #include I specified:
#include ".././ode-0.11.1/include/ode/ode.h"
#include ".././ode-0.11.1/include/drawstuff/drawstuff.h"
Since the library is just located in my home directory and not in the standard location. I go to compile my code but:
.././ode-0.11.1/include/ode/ode.h:28:27: fatal error: ode/odeconfig.h: No such
file or directory
Looking at ode.h, it includes a bunch of other headers all located in the same directory, but using the bracket syntax instead of quotes. I'm guessing this has something to do with why gcc can't locate the other headers. I've looked at the makefiles but don't know enough to figure out what my issue is. Why is my compilation not working? A detailed/thorough answer would be appreciated since I want to learn how this all works (linking, includes, make, etc.).
Edit: So I figured out how to include the headers correctly, now I need to figure out how to link to the library definitions for the functions...
Edit2: Still can't figure out how to link to my code. The compiled static libraries get dumped in '~/ode-0.11.1/ode/src/.libs' and '~/ode-0.11.1/drawstuff/src/.libs' for the drawing functions.
Edit3: I think I figured it out. I wasn't using the -l option correctly, and it seems like it has to go AFTER the files that reference functions from the libraries I'm linking to.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在编译行中设置包含目录,
例如
或者更好的是在这个 cas 中是绝对路径
然后在代码中包含类似
因此将从同一目录访问 ode.h 中包含的所有文件。您的示例 ode/odeconfig.h 将被发现,因为 ode 是 -I 参数中包含路径的子目录。
链接类似,但两个部分都是在命令行上完成的。这两部分是由 -l 变量给出的文件和由 -L 参数给出的 lib 所在的目录。另外,如果库是 libode.dylib,您只需输入名称即可,例如 ode。
所以命令行是
You need to set the include directory in the compile line
e.g.
or better in this cas an absolute path
Then in code include like
Thus all the files included from ode.h will be accessed from the same directory. Your example ode/odeconfig.h would be found as ode is a subdirectoy from the include path in the -I parameter.
Linking is similar but both parts done on the command line .The two parts are file given by a -l variable and the directory the lib is in by -L parameter. Also if the library is say libode.dylib the you just meed the name e.g. ode.
So command line is
转到目录 ode-0.11.1
这将是该项目的所在地。
所以包含文件属于目录 ode-0.11.1/include
在这种情况下,您的源代码应该像
myprog.c
编译命令应该是这样的一行:
Commandexecuted in directory ode-0.11.1/include
go to directory ode-0.11.1
This will be the project's home.
So include files belong to directory ode-0.11.1/include
In this case your source should be like
myprog.c
compilation command should be a line like:
Command executed in directory ode-0.11.1/include