原文連接 https://erlang.mk/guide/ports.htmlhtml
Erlang.mk can not only build Erlang projects, but also the C code that some projects come with, like NIFs and port drivers.windows
Erlang.mk不只能夠構建Erlang項目,還能夠構建一些C代碼編寫的工程,好比NIF和端口驅動程序。app
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
.ide
有兩種方式構建C代碼:使用自定義Makefile,或者使用Erlang.mk構造。這時當你運行 make
,Erlang.mk 將根據須要構建C代碼。ui
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:this
須要編譯的C源碼應該位於 $(C_SRC_DIR) 目錄下, $(C_SRC_DIR) 的默認值是c_src/,若是你須要修改它,你只須要在你的Makefile引入Erlang.mk前設置 C_SRC_DIR 變量。spa
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:code
當這個目錄存在時,Erlang.mk會自動建立一個名爲 $(C_SRC_ENV) 的文件,默認狀況下這個文件是 $(C_SRC_DIR)/env.mk ,你也能夠改變它:orm
C_SRC_ENV = $(C_SRC_DIR)/erlang_env.mk
It contains a few variable definitions for the environment used for the build:htm
它包含一些用於構建環境的變量的定義:
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靜態庫的路徑。
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
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
來手動列出要編譯的文件。
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