mkmf在编译C扩展名时会忽略子文件夹中的文件

Mat*_*ira 5 c ruby mkmf ruby-c-extension

我想这样组织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
Run Code Online (Sandbox Code Playgroud)

我无法让此设置正确使用mkmfnative_extension/lib中包含的中的文件native_extension.c,将被完全忽略。

构建扩展时,仅native_extension.{h,c}会进行编译,并且native_extension.{so,dll}在尝试运行扩展程序时出现不完整的符号查找错误。

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

小智 7

您可以使用带有“ 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)
Run Code Online (Sandbox Code Playgroud)


Mic*_*ger 4

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