8

我想像这样组织 C 源代码:

+ /
|
|___ + ext
|    |
|    |___ + native_extension
|         |
|         |___ + lib
|         |    |
|         |    |___ (Source files are kept in here - may contain sub-folders)
|         |
|         |___ native_extension.c
|         |___ native_extension.h
|         |___ extconf.rb
|
|___ + lib
|    |
|    |___ (Ruby source code)
|
|___ Rakefile

我无法让此设置与mkmf. native_extension/lib中包含的文件native_extension.c将被完全忽略。

当我构建扩展时,只native_extension.{h,c}编译,我得到一个不完整native_extension.{so,dll}的,当我尝试运行它时给我符号查找错误。

有什么办法可以使这项工作?

4

2 回答 2

10

您可以使用“extconf.rb”中来自其他文件夹的源文件,如下所示:

require 'mkmf'

extension_name = 'native_extension'
dir_config(extension_name)

# enum all source files
$srcs = ["native_extension.c", "lib/file.c"]

# add include path to the internal folder
# $(srcdir) is a root folder, where "extconf.rb" is stored
$INCFLAGS << " -I$(srcdir)/lib"

# add folder, where compiler can search source files
$VPATH << "$(srcdir)/lib"

create_makefile(extension_name)
于 2016-03-07T11:09:24.633 回答
4

虽然您可以传递第二个参数来make_makefile指定不同的源目录(例如,make_makfile('native_extension', 'lib')),但这会导致它不包含您的native_extension.c文件。查看 mkmf.rb 的源代码,除了自己重写生成的 Makefile 之外,似乎没有任何方法可以让它在两个地方都显示出来。

于 2011-10-09T19:29:17.557 回答