emscripten-core / emscripten-core/emscripten
problem with build cairo using emscripten(works fine if I just use cmake)
- Dominant language
- C++
- Stars
- 27.6k
- Forks
- 3.6k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 105
Description
I built a simple demo for rendering a simple arch in window using SDL2 + cairo. But I get some error when I try to do it with emscripten and get the error like `error: undefined symbol: cairo_set_line_width (referenced by top-level compiled C/C++ code)`.
Here is the `CMakeLists.txt`
```Cmake
cmake_minimum_required(VERSION 3.10)
project(Karaoke)
# cmake
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
# Source files
set(SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/source")
set(EXTERNAL_DIR "${CMAKE_CURRENT_SOURCE_DIR}/external")
set(SOURCES "${SRC_DIR}/main.cpp")
file(GLOB SRC
CONFIGURE_DEPENDS
"include/*.h"
"source/*.cpp"
)
add_executable(main ${SRC})
target_include_directories(main PRIVATE "include")
target_compile_features(main PUBLIC cxx_std_17)
set_target_properties(main PROPERTIES CXX_EXTENSIONS OFF)
# # OpenGL
# find_package(OpenGL REQUIRED)
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
# Cairo
if (${CMAKE_SYSTEM_NAME} STREQUAL "Emscripten")
else ()
find_package(Cairo)
if(CAIRO_FOUND)
message("Cairo found")
else()
message(FATAL_ERROR "Cairo not found")
endif()
endif()
target_include_directories(main PRIVATE ${CAIRO_INCLUDE_DIRS})
target_link_libraries(main ${CAIRO_LIBRARIES})
message(${CAIRO_INCLUDE_DIRS} ${CAIRO_LIBRARIES})
# SDL 2
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DCMAKE_EXPORT_COMPILE_COMMANDS=1")
if (${CMAKE_SYSTEM_NAME} STREQUAL "Emscripten")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s USE_SDL=2 -s USE_SDL_IMAGE=2")
set(SDL2_LIBRARIES "-s USE_SDL=2")
set(SDL2_IMAGE_LIBRARIES "-s USE_SDL_IMAGE=2")
set(SDL2_TTF_LIBRARIES "-s USE_SDL_TTF=2")
else ()
include(FindPkgConfig)
find_package(SDL2 REQUIRED)
find_package(SDL2_Image REQUIRED)
find_package(SDL2_ttf REQUIRED)
pkg_search_module(SDL2 REQUIRED sdl2)
pkg_search_module(SDL2IMAGE REQUIRED SDL2_image>=2.0.0)
pkg_search_module(SDL2TTF REQUIRED SDL2_ttf>=2.0.0)
include_directories(${SDL2_INCLUDE_DIRS} ${SDL2IMAGE_INCLUDE_DIRS} ${SDL2TFF_INCLUDE_DIRS})
target_link_libraries(main ${SDL2_LIBRARIES} ${SDL2_IMAGE_LIBRARIES} ${SDL2_TTF_LIBRARIES})
endif ()
## end part
cmake_policy (SET CMP0072 NEW)
# Assets
file(COPY assets DESTINATION ${CMAKE_BINARY_DIR})
```
And below is the shell script I used for dev.
```sh
#!/bin/bash
emcmake cmake -S . -B build -D CAIRO_LIBRARIES=/usr/local/Cellar/cairo/1.16.0_4/lib/libcairo.dylib -D CAIRO_INCLUDE_DIRS=/usr/local/Cellar/cairo/1.16.0_4/include/cairo
cd build
emmake make
```
But after I run the command `cmmake make`, I get the error log:
```
Scanning dependencies of target main
[ 33%] Building CXX object CMakeFiles/main.dir/source/MainWindow.cpp.o
[ 66%] Building CXX object CMakeFiles/main.dir/source/main.cpp.o
[100%] Linking CXX executable main.js
error: undefined symbol: cairo_arc (referenced by top-level compiled C/C++ code)
warning: Link with `-s LLD_REPORT_UNDEFINED` to get more information on undefined symbols
warning: To disable errors for undefined symbols use `-s ERROR_ON_UNDEFINED_SYMBOLS=0`
warning: _cairo_arc may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: cairo_create (referenced by top-level compiled C/C++ code)
warning: _cairo_create may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: cairo_fill (referenced by top-level compiled C/C++ code)
warning: _cairo_fill may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: cairo_image_surface_create_for_data (referenced by top-level compiled C/C++ code)
warning: _cairo_image_surface_create_for_data may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: cairo_rectangle (referenced by top-level compiled C/C++ code)
warning: _cairo_rectangle may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: cairo_set_line_width (referenced by top-level compiled C/C++ code)
warning: _cairo_set_line_width may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: cairo_set_source_rgba (referenced by top-level compiled C/C++ code)
warning: _cairo_set_source_rgba may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: cairo_stroke (referenced by top-level compiled C/C++ code)
warning: _cairo_stroke may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
error: undefined symbol: cairo_surface_set_device_scale (referenced by top-level compiled C/C++ code)
warning: _cairo_surface_set_device_scale may need to be added to EXPORTED_FUNCTIONS if it arrives from a system library
Error: Aborting compilation due to previous errors
em++: error: '/Users/someone/lab/emsdk/node/12.18.1_64bit/bin/node /Users/someone/lab/emsdk/upstream/emscripten/src/compiler.js /var/folders/gm/1xp7t1wj1lx_8298gt0xxf2h0000gp/T/tmp3k7i7vdm.txt' failed (1)
make[2]: *** [main.js] Error 1
make[1]: *** [CMakeFiles/main.dir/all] Error 2
make: *** [all] Error 2
```
---
but if I run the **native cmake** like below
```sh
#!/bin/bash
cmake -S . -B build
cmake --build build -j
./build/main
```
It just works fine.

Contributor guide
Assessment
This issue has not been assessed yet.