第8章 NIF和端口驱动程序

原文链接  https://erlang.mk/guide/ports.html

 

Chapter 8. NIFs and port drivers

 

Erlang.mk can not only build Erlang projects, but also the C code that some projects come with, like NIFs and port drivers.

Erlang.mk不仅可以构建Erlang项目,还可以构建一些C代码编写的工程,比如NIF和端口驱动程序。

 

There are two ways to build the C code: using a custom Makefile, or making Erlang.mk do it directly. The C code will be built as needed when you run make.

有两种方式构建C代码:使用自定义Makefile,或者使用Erlang.mk构造。这时当你运行 make,Erlang.mk 将根据需要构建C代码。

 

8.1. C source code location and Erlang environment

8.1. C源码的位置和Erlang环境

 

The C source code should be located in the $(C_SRC_DIR) directory. It defaults to c_src/. Should you need to modify it, all you need to do is to set the variable in your Makefile before including Erlang.mk:

需要编译的C源码应该位于 $(C_SRC_DIR)  目录下, $(C_SRC_DIR) 的默认值是c_src/,如果你需要修改它,你只需要在你的Makefile引入Erlang.mk前设置 C_SRC_DIR 变量。

C_SRC_DIR = $(CURDIR)/my_nif_source

 

When this directory exists, Erlang.mk will automatically create a file named $(C_SRC_ENV). This file defaults to $(C_SRC_DIR)/env.mk. This can also be changed:

当这个目录存在时,Erlang.mk会自动创建一个名为 $(C_SRC_ENV) 的文件,默认情况下这个文件是 $(C_SRC_DIR)/env.mk ,你也可以改变它:

C_SRC_ENV = $(C_SRC_DIR)/erlang_env.mk

 

It contains a few variable definitions for the environment used for the build:

它包含一些用于构建环境的变量的定义:

 

ERTS_INCLUDE_DIR

        Path to the ERTS include files (erl_driver.h, erl_nif.h and more).

        ERTS的路径,里面包含erl_driver.h、erl_nif.h等文件。

 

ERL_INTERFACE_INCLUDE_DIR

        Path to the Erl_Interface include files (ei.h and related).

        Erl_Interface的路径,里面包含ei.h及其相关文件。

 

ERL_INTERFACE_LIB_DIR

        Path to the Erl_Interface static libraries.

        Erl_Interface静态库的路径。

 

8.2. Using a custom Makefile

8.2. 使用自定义Makefile

 

Erlang.mk will automatically run make if it detects a Makefile in $(C_SRC_DIR)/Makefile.

如果在 $(C_SRC_DIR) 目录下检测到Makefile文件,Erlang.mk就会自动运行make。

 

The Makefile should have at least two targets: a default target (which can be anything, for example all) which is invoked when building the C code, and a clean target invoked when cleaning it.

这个Makefile至少应该有两个targets:一个默认target在构建C代码时调用(可以是任何东西,例如all),一个是清理项目的 clean target。

 

You can include the env.mk file to benefit from the Erlang environment detection:

你可以通过包含env.mk文件而从Erlang环境检测中受益:

include env.mk

 

8.3. Using Erlang.mk directly

8.3. 直接使用Erlang.mk

 

You don’t need to write a Makefile to build C source code, however. Erlang.mk comes with rules to build both shared libraries and executables, using the source files it finds in $(C_SRC_DIR).

但是,你不需要编写Makefile来构建C代码。 Erlang.mk附带规则来构建共享库和可执行文件,使用它在 $(C_SRC_DIR) 中找到的源文件。

 

By default, Erlang.mk will create a shared library. To change this and create an executable instead, put this in your Makefile before including Erlang.mk:

默认情况下,Erlang.mk会将 $(C_SRC_DIR) 的C代码构建为一个共享库,要改变这个行为并创建一个可执行文件,你可以在包含 Erlang.mk 前把下面这行加入到 Makefile:

C_SRC_TYPE = executable

 

The generated file name varies depending on the type of project you have (shared library or executable) and on the platform you build the project on.

生成的文件名取决于你拥有的项目类型(共享库或可执行文件)及构建项目的平台。

 

For shared libraries, the generated file name will be $(C_SRC_OUTPUT)$(C_SRC_SHARED_EXTENSION), with the default being $(CURDIR)/priv/$(PROJECT) followed by the extension: .dll on Windows, .so everywhere else.

对于动态库,生成的文件名将是$(C_SRC_OUTPUT)$(C_SRC_SHARED_EXTENSION),默认是 $(CURDIR)/priv/$(PROJECT),在windows下后面是扩展名:.dll,其他地方则是 .so

 

For executables, the generated file name is $(C_SRC_OUTPUT)$(C_SRC_EXECUTABLE_EXTENSION), with the same default except for the extension: .exe on Windows, and otherwise nothing.

对于可执行文件,生成的文件名是$(C_SRC_OUTPUT)$(C_SRC_EXECUTABLE_EXTENSION),除了在Windows上有扩展名.exe ,其他系统都没有后缀。

 

Erlang.mk sets appropriate compile and linker flags by default. These flags vary depending on the platform, and can of course be overriden.

Erlang.mk默认设置适当的编译和链接器标志,这些标志因平台而异,当然也可以被重写。

 

CC

        The compiler to be used.

        要使用的编译器。

 

CFLAGS

        C compiler flags.

        C编译器标志。

 

CXXFLAGS

        C++ compiler flags.

         C++编译器标志。

 

LDFLAGS

        Linker flags.

        链接标志。

 

LDLIBS

        Libraries to link against.

        需要链接的库。

 

The source files are automatically gathered from the contents of $(C_SRC_DIR). Erlang.mk looks for .c, .C, .cc and .cpp source files. You can define the variable SOURCES to manually list the files to compile.

Erlang.mk自动在 $(C_SRC_DIR) 目录下查找并收集带有 .c,.C,.cc和.cpp 后缀的源文件,你也可以通过定义变量 SOURCES 来手动列出要编译的文件。

 

8.4. Propagating compile and linker flags to sub-Makefiles

8.4. 将编译和链接器标志传给子Makefile

 

In some cases it might be necessary to propagate the flags you just defined to the sub-Makefiles of your local project. You generally can’t just export those as this could impact the building of dependencies.

在某些情况下,可能需要将刚刚定义的标志传播到本地项目的子Makefiles。 你通常不能只导出这些,因为这可能会影响依赖关系的构建。

 

Makefiles allow you to export variables for specific targets. When doing this, the variables will be exported only when this target runs, and not for other targets. It is therefore possible to export them when building the C code without impacting other build steps.

Makefiles允许你导出特定目标的变量。 这样做时,指定的变量只会在该target运行时导出,其他targets运行则不会导出。 因此,可以在构建C代码时将其导出,而不影响其他构建步骤。

 

By adding this to your Makefile all five variables will be made available to sub-Makefiles when building C code:

通过将下面几行添加到你的Makefile中,当构建C代码时,这五个变量都会被导出给子Makefiles:

app-c_src: export CC +=
app-c_src: export CFLAGS +=
app-c_src: export CPPFLAGS +=
app-c_src: export LDFLAGS +=
app-c_src: export LDLIBS +=

 

Appending an empty string to the existing value is necessary because Makefiles expect an assignment for target-specific exports. Alternatively you can set a new value:

将一个空字符串附加到现有的值上是必要的,因为Makefiles期望指出具体需要导出的target。 或者,你也可以设置一个新的值:

app-c_src: export CFLAGS = -O3

转载于:https://my.oschina.net/u/258912/blog/1605133

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值