lvgl / lv_port_linux

LVGL configured to work with a standard Linux framebuffer
MIT License
260 stars 160 forks source link

Trying to build as a static binary #65

Open ric96 opened 6 days ago

ric96 commented 6 days ago

Hi all,

Trying to build the demo as a static binary with the following cmake changes

diff --git a/CMakeLists.txt b/CMakeLists.txt
index e13cf29..9c0b44d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -8,13 +8,18 @@ endforeach()

 # Uncomment if the program needs debugging
 #set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 -ggdb")
-
+set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
+set(BUILD_SHARED_LIBS OFF)
+SET(CMAKE_EXE_LINK_DYNAMIC_C_FLAGS "-Wl,-Bstatic")
+SET(CMAKE_EXE_LINK_DYNAMIC_CXX_FLAGS "-Wl,-Bstatic")
+set(CMAKE_EXE_LINKER_FLAGS "-static")
 set(CMAKE_C_STANDARD 99) # LVGL officially supports C99 and above
 set(CMAKE_CXX_STANDARD 17) #C17
 set(CMAKE_CXX_STANDARD_REQUIRED ON)
 set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
 add_subdirectory(lvgl)
 target_include_directories(lvgl PUBLIC ${PROJECT_SOURCE_DIR})
+link_libraries("-static-libgcc -static-libstdc++")

 if (LV_USE_LINUX_DRM)

@@ -23,7 +28,7 @@ if (LV_USE_LINUX_DRM)

     target_include_directories(lvgl PUBLIC ${LIBDRM_INCLUDE_DIRS})
     add_executable(lvglsim main.c mouse_cursor_icon.c)
-    target_link_libraries(lvglsim lvgl lvgl::examples lvgl::demos lvgl::thorvg ${LIBDRM_LIBRARIES} m pthread)
+    target_link_libraries(lvglsim lvgl lvgl::examples lvgl::demos lvgl::thorvg ${LIBDRM_LIBRARIES} m pthread -static-libgcc -static-libstdc++)

 elseif (LV_USE_SDL)

@@ -34,7 +39,7 @@ elseif (LV_USE_SDL)
     target_include_directories(lvgl PRIVATE ${SDL2_INCLUDE_DIRS})
     target_include_directories(lvgl PRIVATE ${SDL2_IMAGE_INCLUDE_DIRS})
     add_executable(lvglsim main.c mouse_cursor_icon.c)
-    target_link_libraries(lvglsim lvgl lvgl::examples lvgl::demos lvgl::thorvg ${SDL2_LIBRARIES} ${SDL2_IMAGE_LIBRARIES} m pthread)
+    target_link_libraries(lvglsim lvgl lvgl::examples lvgl::demos lvgl::thorvg ${SDL2_LIBRARIES} ${SDL2_IMAGE_LIBRARIES} m pthread -static-libgcc -static-libstdc++)

 elseif (LV_USE_WAYLAND)

@@ -55,13 +60,17 @@ elseif (LV_USE_WAYLAND)
         mouse_cursor_icon.c backends/wayland.c)
     target_compile_definitions(lvglsim PRIVATE LV_CONF_INCLUDE_SIMPLE)
     target_link_libraries(lvglsim lvgl lvgl::examples lvgl::demos lvgl::thorvg m
-        wayland-client wayland-cursor xkbcommon)
+        wayland-client wayland-cursor xkbcommon -static-libgcc -static-libstdc++)

 else()

     # No specific build steps required for FBDEV
     add_executable(lvglsim main.c mouse_cursor_icon.c)
-    target_link_libraries(lvglsim lvgl lvgl::examples lvgl::demos lvgl::thorvg m pthread)
+    target_link_libraries(lvglsim lvgl lvgl::examples lvgl::demos lvgl::thorvg m pthread -static-libgcc -static-libstdc++)
+    set_target_properties(lvglsim PROPERTIES
+      LINK_SEARCH_START_STATIC ON
+      LINK_SEARCH_END_STATIC ON
+    )
 endif()

 add_custom_target (run COMMAND ${EXECUTABLE_OUTPUT_PATH}/lvglsim DEPENDS lvglsim)
diff --git a/Makefile b/Makefile
index e91e403..73e7b26 100644
--- a/Makefile
+++ b/Makefile
@@ -10,8 +10,8 @@ WARNINGS      := -Wall -Wshadow -Wundef -Wmissing-prototypes -Wno-discarded-qualifie
                    -Wsizeof-pointer-memaccess -Wno-format-nonliteral -Wno-cast-qual -Wunreachable-code -Wno-switch-default -Wreturn-type -Wmultichar -Wformat-security \
                    -Wno-ignored-qualifiers -Wno-error=pedantic -Wno-sign-compare -Wno-error=missing-prototypes -Wdouble-promotion -Wclobbered -Wdeprecated -Wempty-body \
                    -Wshift-negative-value -Wstack-usage=2048 -Wno-unused-value -std=gnu99
-CFLAGS             ?= -O3 -g0 -I$(LVGL_DIR)/ $(WARNINGS)
-LDFLAGS        ?= -lm
+CFLAGS             ?= -O3 -g0 -static -I$(LVGL_DIR)/ $(WARNINGS)
+LDFLAGS        ?= -lm -static
 BIN            = main
 BUILD_DIR      = ./build
 BUILD_OBJ_DIR  = $(BUILD_DIR)/obj
diff --git a/lvgl b/lvgl
--- a/lvgl
+++ b/lvgl
@@ -1 +1 @@
-Subproject commit 8d47460580c6e17ff800aaac2dbbb11c3520cc44
+Subproject commit 8d47460580c6e17ff800aaac2dbbb11c3520cc44-dirty

However at the final link stage I get this error:

[100%] Linking CXX executable /home/ric/lv_port_linux/bin/lvglsim
/usr/bin/cmake -E cmake_link_script CMakeFiles/lvglsim.dir/link.txt --verbose=1
/usr/lib64/ccache/c++ -static CMakeFiles/lvglsim.dir/main.c.o CMakeFiles/lvglsim.dir/mouse_cursor_icon.c.o -o /home/ric/lv_port_linux/bin/lvglsim  -static-libgcc -static-libstdc++ lib/liblvgl.a lib/liblvgl_examples.a lib/liblvgl_demos.a lib/liblvgl_thorvg.a -lm -lpthread -static-libgcc -static-libstdc++ lib/liblvgl.a -static 
/usr/bin/ld: cannot find -lstdc++: No such file or directory
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/lvglsim.dir/build.make:121: /home/ric/lv_port_linux/bin/lvglsim] Error 1
make[2]: Leaving directory '/home/ric/lv_port_linux/build'
make[1]: *** [CMakeFiles/Makefile2:113: CMakeFiles/lvglsim.dir/all] Error 2
make[1]: Leaving directory '/home/ric/lv_port_linux/build'
make: *** [Makefile:139: all] Error 2
make: Leaving directory '/home/ric/lv_port_linux/build'

I am confused what is triggering the -lstdc++ flag instead of its static alternative.