mkmf ignores files in sub-folders when it compiles the C extension

纵然是瞬间 提交于 2019-11-30 17:01:57

问题


I'd like to organize the C source code like this:

+ /
|
|___ + 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

I'm having trouble getting this setup to work correctly with mkmf. The files in native_extension/lib, which are included by native_extension.c, are being completely ignored.

When I build the extension, only native_extension.{h,c} are compiled, and I get an incomplete native_extension.{so,dll} that gives me symbol lookup errors when I try to run it.

Any way to make this work?


回答1:


While you can pass a second argument to make_makefile to specify a different source directory (e.g., make_makfile('native_extension', 'lib')), that would cause it not to include your native_extension.c file. Looking at the source for mkmf.rb, it doesn't appear there's any way to make it look in both places short of rewriting the generated Makefile yourself.




回答2:


You can use source files from another folders with "extconf.rb" like this:

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)


来源:https://stackoverflow.com/questions/7698192/mkmf-ignores-files-in-sub-folders-when-it-compiles-the-c-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!