设置环境变量并在 Makefile 中包含链接目录
我有一个 C 文件,其中包含一个标头,该标头本身包含“jni.h”。我目前正在使用以下命令编译该文件:
gcc hdfs_test.c -I/HDFS_HOME/hdfs/src/c++/libhdfs -I/usr/lib/jvm/default-java/include -L/HDFS_HOME/hdfs/src/c++/libhdfs -L/HDFS_HOME/build/c++/Linux-i386-32/lib -L/usr/lib/jvm/default-java/jre/lib/i386/server -ljvm -lhdfs -o hdfs_test
运行生成的 .o 文件、CLASSPATH 和 JAVAHOME 的环境变量。
现在,我想在使用 make 文件的现有 C 项目中使用此文件。该项目是Postgresql。在这个项目中使用了 Makefile。我想将所需的指令添加到要添加文件的文件夹中的 makefile 中,以便我可以在项目中运行它。
当前的 makefile 具有以下内容:
subdir = src/backend/storage/smgr
top_builddir = ../../../..
include $(top_builddir)/src/Makefile.global
OBJS = md.o smgr.o smgrtype.o
include $(top_srcdir)/src/backend/common.mk
为了用项目编译我的 c 文件,我应该在 makefile 中添加什么?
谢谢
* 编辑 1 *
我可能应该说明我想要表达的内容做。到目前为止,我已将多个源文件添加到 postgres SMGR 模块(源目录)。 只需将 fileName.o 添加到 OBJS 文件列表中,它们就可以正常工作。例如,如果我添加了 Ac,我会将 Ao 添加到 OBJS 列表中,该文件将被编译并添加到项目中。
现在我尝试添加的新文件是 hdfs_test.c 这与我之前添加的其他源文件不同。不同之处在于它不使用简单的 gcc 命令进行编译,而是需要我上面显示的命令。 我尝试通过将其添加到我的 Makefile 中来手动生成 .o 文件:
OBJS = md.o smgr.o smgrtype.o hdfs_FD.o hdfsManager.o smgrWrapper.o hdfs_test.o
include $(top_srcdir)/src/backend/common.mk
hdfs_test.o : hdfs_test.c
gcc hdfs_test.c -I/HDFS_HOME/hdfs/src/c++/libhdfs -I/usr/lib/jvm/default-java/include -L/HDFS_HOME/hdfs/src/c++/libhdfs -L/HDFS_HOME/build/c++/Linux-i386-32/lib -L/usr/lib/jvm/default-java/jre/lib/i386/server -ljvm -lhdfs -o hdfs_test
当 Make 时,我收到以下错误:
/usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 0 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o( .debug_info): 重定位 1 的符号索引 13 无效 /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): 重定位 2 具有无效的符号索引 2 /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): 重定位 3 具有无效的符号索引 2 /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): 重定位 4 具有无效的符号索引 12 /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): 重定位 5 具有无效的符号索引 14 /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): 重定位 6 具有无效的符号索引 14 /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): 重定位 7 具有无效的符号索引 14 /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): 重定位 8 具有无效的符号索引 2 /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): 重定位 9 具有无效的符号索引 2 /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): 重定位 10 具有无效的符号索引 13 /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): 重定位 11 具有无效的符号索引 14 /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): 重定位 12 具有无效的符号索引 14 /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): 重定位 13 具有无效的符号索引 14 /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): 重定位 14 具有无效的符号索引 14 /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): 重定位 15 具有无效的符号索引 14 /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): 重定位 16 具有无效的符号索引 14 /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): 重定位 17 具有无效的符号索引 14 /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): 重定位 18 具有无效的符号索引 14 /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): 重定位 19 具有无效的符号索引 14 /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): 重定位 20 具有无效的符号索引 14 /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): 重定位 21 具有无效的符号索引 14 /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): 重定位 22 具有无效的符号索引 22 /usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crt1.o:在函数 _start' 中: (.text+0x18):未定义对 main' 的引用 Collect2: ld 返回 1 退出状态 make: * [tati.o] 错误 1
希望这能澄清问题。
I have a C file that includes a header that itself includes "jni.h". I'm currently compiling the file using the command:
gcc hdfs_test.c -I/HDFS_HOME/hdfs/src/c++/libhdfs -I/usr/lib/jvm/default-java/include -L/HDFS_HOME/hdfs/src/c++/libhdfs -L/HDFS_HOME/build/c++/Linux-i386-32/lib -L/usr/lib/jvm/default-java/jre/lib/i386/server -ljvm -lhdfs -o hdfs_test
to run the resulting .o file, the environment variables for CLASSPATH and JAVAHOME.
Now, I want to use this file in an existing C project where they use make files. The project is Postgresql. In this project Makefiles are used. I want to add the needed instructions to the makefile in the folder where I'm adding the file, so that I can run it with the project.
The current makefile has the following:
subdir = src/backend/storage/smgr
top_builddir = ../../../..
include $(top_builddir)/src/Makefile.global
OBJS = md.o smgr.o smgrtype.o
include $(top_srcdir)/src/backend/common.mk
What should I add to the makefile in order to compile my c file with the project?
Thanks
* Edit 1 *
I probably should have stated what I was trying to do. So far, I added multiple source files to the postgres SMGR module (source directory).
They all worked fine by just adding the fileName.o to the list of OBJS files. For example if I added A.c, I would add A.o to the OBJS list and the file will be compiled and added to the project.
Now the new file that I'm trying to add is hdfs_test.c
this is different than the other source files that I added previously. The difference is that it doesn't compile using simple gcc command, but needs the command that I've shown above.
I tried to manually generate the .o file manually by adding this to my Makefile:
OBJS = md.o smgr.o smgrtype.o hdfs_FD.o hdfsManager.o smgrWrapper.o hdfs_test.o
include $(top_srcdir)/src/backend/common.mk
hdfs_test.o : hdfs_test.c
gcc hdfs_test.c -I/HDFS_HOME/hdfs/src/c++/libhdfs -I/usr/lib/jvm/default-java/include -L/HDFS_HOME/hdfs/src/c++/libhdfs -L/HDFS_HOME/build/c++/Linux-i386-32/lib -L/usr/lib/jvm/default-java/jre/lib/i386/server -ljvm -lhdfs -o hdfs_test
When Make, I'm getting the following error:
/usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 0 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 1 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 4 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 5 has invalid symbol index 14
/usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 6 has invalid symbol index 14
/usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 7 has invalid symbol index 14
/usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 8 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 9 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 10 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 11 has invalid symbol index 14
/usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 12 has invalid symbol index 14
/usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 13 has invalid symbol index 14
/usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 14 has invalid symbol index 14
/usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 15 has invalid symbol index 14
/usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 16 has invalid symbol index 14
/usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 17 has invalid symbol index 14
/usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 18 has invalid symbol index 14
/usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 19 has invalid symbol index 14
/usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 20 has invalid symbol index 14
/usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 21 has invalid symbol index 14
/usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 22 has invalid symbol index 22
/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crt1.o: In function _start':
main'
(.text+0x18): undefined reference to
collect2: ld returned 1 exit status
make: * [tati.o] Error 1
Hope this clarifies things.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从外观上看,postgress 使用 automake 构建环境。
您对 Make 的了解似乎很有限,因此我建议您阅读以下内容:
并执行其中的示例,并为您的 hdfs_test 修改它们。一旦您了解了如何做到这一点,您就能够回答您的问题。当您在那里遇到问题时,我很乐意帮助您解决。
postgress uses automake build environment from the looks of it.
Your knowledge of just Make seems to be limited, so i would suggest that you read up on the following.:
and do the examples in there and them modify them for your hdfs_test. Once you understand how to do that, you'll be able to answer your question. When you stumble into an issue there, I'll be happy to help you resolve it.
这实际上取决于您的代码到底想要做什么,以及 JVM 和 C++ 扮演的角色。即使你能构建它,它也可能根本不起作用。您或许应该与 PostgreSQL 开发人员讨论细节。您最好将代码构建为共享库并将其安装为扩展。
This really depends on what exactly your code is trying to do, and why role the JVM and C++ play. It might not work at all, even if you get it to build. You should probably discuss the details with the PostgreSQL developers. You might also be better off building your code as a shared library and installing it as an extension.
由多个目录中的多个文件组成的 Makefile 通常很难理解,并且依赖大量黑魔法才能正常工作,因此仅向我们展示一个小文件可能没有多大意义。
也就是说,我首先向 OBJS 添加一个新的 .o 文件,然后看看会发生什么。可能它会假设有一个具有相同基本名称的源文件,并且 Just Work。该文件不提供任何依赖关系信息,因此它可能会自动生成该信息,而无需您输入任何内容。
另一方面,整个系统可能是垃圾......
Makefiles consisting of multiple files in multiple directories are typically very hard to understand, and rely on lots of black magic to work properly, so showing us just one small file is probably not very meaningful.
That said, I'd start by just adding a new .o file to
OBJS
, and see what happens. Probably it'll assume that there's a source file of the same basename, and Just Work. The file doesn't give any dependency information, so it will probably auto-generate that without any input from you.On the other hand, the whole system might be rubbish ....