使用Emscripten(Emcmake)时指定选项的问题

时间:2017-02-21 22:42:14

标签: c++ cmake emscripten

我的问题是关于将CMakeLists.txt与Emscriptem一起使用并指定输出类型以及命令行选项。

我想采用一个简单的Emscripten命令,例如:emcc file.cpp -o file.html --preload-file asset_dir/并将其更改为我可以在CMake系统中指定的内容。我尝试了重命名可执行文件的天真方法,以获得html的扩展但是没有用。我也尝试使用-D--preload-file:PATH=asset_dir,但也没有用。

我的CMakeLists.txt文件很小,包含在下面。我使用命令emcmake cmake -G"Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER=em++ .. && emmake make来构建它。

CMAKE_MINIMUM_REQUIRED(VERSION 3.2.0 FATAL_ERROR)
PROJECT(ProjJS)

# Set typical CMAKE settings
SET(CMAKE_BUILD_TYPE_INIT "Release")
SET(CMAKE_VERBOSE_MAKEFILE OFF CACHE BOOL "Turn on Verbose Makefiles" FORCE)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules")

SET(CMAKE_CXX_STANDARD 11)
SET(CMAKE_CXX_STANDARD_REQUIRED ON)

SET(PROJ_SOURCES
    hello.cpp
    ....
)

set(NAME_OF_EXE "ProjJS")

set(BOOST_LIB "boost")
set(BOOST_INCLUDE_PATH "${CMAKE_SOURCE_DIR}/extlibs/")
set(BOOST_LIB_PATH "${CMAKE_SOURCE_DIR}/extlibs/boost/libs/")

add_subdirectory(extlibs/boost)


SET(BOOST_PROGRAM_OPTIONS_SOURCES
    ${BOOST_LIB_PATH}/program_options/cmdline.cpp
    ${BOOST_LIB_PATH}/program_options/config_file.cpp
    ....
)

SET(BOOST_SYSTEM_SOURCES
    ${BOOST_LIB_PATH}/system/error_code.cpp
)

ADD_EXECUTABLE(${NAME_OF_EXE} ${PROJ_SOURCES})
add_library(${BOOST_LIB} STATIC ${BOOST_PROGRAM_OPTIONS_SOURCES} ${BOOST_SYSTEM_SOURCES})
TARGET_INCLUDE_DIRECTORIES(${BOOST_LIB} PUBLIC "${BOOST_INCLUDE_PATH}")

TARGET_LINK_LIBRARIES(${NAME_OF_EXE} PUBLIC ${BOOST_LIB})
TARGET_INCLUDE_DIRECTORIES(${NAME_OF_EXE} BEFORE PRIVATE "${CMAKE_SOURCE_DIR}/include")
TARGET_INCLUDE_DIRECTORIES(${NAME_OF_EXE} BEFORE PRIVATE "${BOOST_INCLUDE_PATH}" ${PROJ_SOURCES})

1 个答案:

答案 0 :(得分:1)

对于输出后缀,它应该起作用:

set(CMAKE_EXECUTABLE_SUFFIX ".html")

完整示例:

cmake_minimum_required(VERSION 2.6 FATAL_ERROR)
project(demo)

set(CMAKE_EXECUTABLE_SUFFIX ".html")

add_executable(hello tests/hello_world.cpp)

这将发出hello.html等。

对于其他标志,我发现的最佳选择是使用target_link_libraries,CMake只是将其附加到链接行,并且可以包含任何内容。例如:

target_link_libraries(binaryen_js "-s MODULARIZE")
target_link_libraries(binaryen_js "-s INITIAL_MEMORY=512MB")

这将打开模块化并将初始内存设置为512MB(例如Binaryen CMake script中的示例)。