在C中的#include中转换绝对路径 [英] Converting an absolute path in #include in C

查看:1092
本文介绍了在C中的#include中转换绝对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从存储库中获取一些代码,以便在我的系统上运行。

I am trying to get some code that I acquired from a repository to work on my system.

在其中一个C文件中,它包含标题的绝对路径:

In one of the C files, it contains an absolute path to a header:

 #include "/home/user/.rvm/rubies/ruby-1.9.2-p290/include/ruby-1.9.1/x86_64-linux/ruby/config.h"

因为我的系统上不存在此文件和目录,所以编译源代码失败。

Because this file and directory does not exist on my system, compiling the source code fails.

我假设如果我将其更改为指向config.h的个人位置,它将成功但在其他人的系统上失败。

I assume if I change this to point at my personal location of config.h, it will succeed but then fail on others' systems.

有没有办法指出一些符号链接,然后系统会使用这个文件的正确位置?处理这种情况的最佳方法是什么?

Is there a way to point at some symbolic link that the system will then use the proper location for such a file? What is the best way to approach this situation?

推荐答案

将其更改为 #includeruby / config .h,然后添加 -I / home / user / .rvm / rubies / ruby​​-1.9.2-p290 / include / ruby​​-1.9.1 / x86_64-你的编译器选项的linux / (或任何你的位置)。这告诉预处理器在查找#includes时将该目录添加到要搜索的目录列表中。

Change it to #include "ruby/config.h", and then add -I/home/user/.rvm/rubies/ruby-1.9.2-p290/include/ruby-1.9.1/x86_64-linux/ (or whatever your location is) to your compiler options. This tells the preprocessor to add that directory to the list of directories to search when looking for #includes.

为了解决可移植性,您可以更改生成makefile的任何内容这个路径作为选项/参数,或者您可以将它放在makefile顶部的变量中并要求人们更改它:

To solve the portability you can then change whatever generates the makefile to take this path as an option/argument, or you can just put it in a variable at the top of the makefile and ask people to change it:

RUBY_LOCATION = /home/user/.rvm/rubies/ruby-1.9.2-p290/include/ruby-1.9.1/x86_64-linux/

CFLAGS = -Wall -I${RUBY_LOCATION}

这篇关于在C中的#include中转换绝对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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