0

Using cmake's set(CMAKE_CXX_VISIBILITY_PRESET hidden) and cxxflags -fvisibility=hidden don't work for me

But other compile options do work, such as -Wl,-Bsymbolic

Compiled macros are also passed in correctly

this is my cmakelists.txt

cmake_minimum_required(VERSION 3.20)
project(multp)

message("generator: ${CMAKE_GENERATOR_PLATFORM}")
message("CMAKE_SIZEOF_VOID_P is ${CMAKE_SIZEOF_VOID_P}")
message("CMAKE_SYSTEM_NAME is ${CMAKE_SYSTEM_NAME}")
enable_testing()
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
set(CMAKE_C_VISIBILITY_PRESET hidden)
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
include(build/cmake/cmake-definition.cmake)
include(build/cmake/Format.cmake)
add_subdirectory(vendors/openssl/1.1.1s)
add_subdirectory(vendors/rapidjson)
add_subdirectory(vendors/gmssl)
add_subdirectory(platform/multp)
target_link_libraries(multp PUBLIC  gmssl rapidjson)
target_compile_options(multp PRIVATE "-Wl,--exclude-libs,ALL" "-Wl,-Bsymbolic" "-fvisibility=hidden")
target_link_options(multp PRIVATE "-Wl,--exclude-libs,ALL" "-Wl,-Bsymbolic" "-fvisibility=hidden")

this is commandline

cmake -DANDROID_ABI=arm64-v8a -DHDMW_CODEC_ENABLED=TRUE -DANDROID_PLATFORM=android-23 -DANDROID_STL=c++_shared -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=G:\Android\SDK\ndk\23.1.7779620\build\cmake\android.toolchain.cmake -DCMAKE_SYSTEM_NAME=Android -B ./cmake-android -S . -G Ninja

None of the hidden symbol operations worked But android.mk, which uses the same ndk, works

APP_STL        := c++_shared

APP_CFLAGS     += -O3 -DANDROID \
                  -Werror=return-type \
                  -Werror=implicit-int \
                  -Werror=incompatible-pointer-types \
                  -Werror=shadow \
                  -Werror=return-stack-address \
                  -Werror=uninitialized \
                  -Werror=format \
                  -Werror=sign-compare \
                  -Werror=int-conversion \
                  -Werror=inconsistent-missing-override \
                  -Werror=implicit-function-declaration \
                  -fvisibility=hidden

ifeq ($(NDK_DEBUG),1)
APP_CFLAGS     += -fsanitize=address \
                  -fno-omit-frame-pointer
APP_LDFLAGS    := -fsanitize=address
endif

ifdef BUILD_VERSION
APP_CFLAGS     += -DBUILD_VERSION=$(BUILD_VERSION)
endif

APP_CPPFLAGS   := $(APP_CFLAGS) -fexceptions -std=c++14

APP_PLATFORM   := android-21

#set as the ndk-build option,
#like this: ndk-build NDK_DEBUG=0/1
#0:release, 1:debug
#APP_OPTIM     := release

#the new ndk not support this setting.
#NDK_TOOLCHAIN_VERSION := 4.9

APP_ABI        := armeabi-v7a arm64-v8a
LOCAL_C_INCLUDES += \
                    $(LOCAL_PATH)/src \
                    $(LOCAL_PATH)/h \
                    $(LOCAL_PATH)/../../vendors/gmssl/include \
                    $(LOCAL_PATH)/../../vendors/openssl/1.1.1s/include

LOCAL_CFLAGS := $(MY_COMMON_DEFS)
LOCAL_CPPFLAGS += -frtti -fexceptions

LOCAL_LDLIBS += -lz -ldl -llog
LOCAL_STATIC_LIBRARIES := libgmssl \
                          libssl \
                          libcrypto 
work-dir> nm -CD libmultp.so
                 U __cmsg_nxthdr
                 U __cxa_allocate_exception
                 U __cxa_atexit
                 U __cxa_begin_catch
                 U __cxa_end_catch
                 U __cxa_finalize
                 U __cxa_free_exception
                 U __cxa_pure_virtual
                 U __cxa_rethrow
                 U __cxa_throw
                 U __errno
                 U __FD_ISSET_chk
                 U __FD_SET_chk
                 U __gxx_personality_v0
                 U __register_atfork
                 U __sF
00000000001cfb9c T crc32_fast(void const*, unsigned long, unsigned int)
0000000000154b90 W void TrimString<CCmIsSpace>(CCmString&, CCmIsSpace)
000000000014bda0 T CalcCRC16_m(unsigned short&, CCmMessageBlock&, unsigned int)
00000000001d0224 T crc32_1byte(void const*, unsigned long, unsigned int)
00000000001403ac T init_config(char const*)
000000000015648c W void LTrimString<CCmIsSpace>(CCmString&, CCmIsSpace)
0000000000156534 W void RTrimString<CCmIsSpace>(CCmString&, CCmIsSpace)
000000000013fb10 T trim_string(char*)
00000000001d04bc T crc32_4bytes(void const*, unsigned long, unsigned int)
00000000001d05fc T crc32_8bytes(void const*, unsigned long, unsigned int)
0000000000141664 T get_local_ip()
00000000001b0e08 T ipv4_enabled()
00000000001b0db8 T ipv6_enabled()
0000000000141308 T resolve_2_ip(char*, char*)
00000000001d09ec T crc32_16bytes(void const*, unsigned long, unsigned int)
00000000001d00d4 T crc32_bitwise(void const*, unsigned long, unsigned int)
00000000001d0fc4 T crc32_combine(unsigned int, unsigned int, unsigned long)
qian
  • 1
  • 1
  • I searched the site and others on GitHub for similar questions https://cmake.org/cmake/help/latest/module/GenerateExportHeader.html#generateexportheader https://stackoverflow.com/questions/72109681/cmake-ignoring-compilation-flags https://github.com/android/ndk/issues/913 I tried all that. It didn't work – qian May 12 '23 at 07:44
  • Have you checked the command lines which directly call the compiler? In case of CMake generated project, these command lines could be printed with `make VERBOSE=1` (https://stackoverflow.com/questions/5820303/how-do-i-force-make-gcc-to-show-me-the-commands). For android.mk these command could be printed with `make V=1` (https://stackoverflow.com/questions/5608007/how-to-see-the-actual-gcc-options-when-building-android-from-source). – Tsyvarev May 12 '23 at 09:19

1 Answers1

0

I compiled a minimal example and found that it worked. After careful inspection I found that I added a compilation flags "-g", it affected my compilation, it was not in my description, it was fine after I removed this. I don't know why that happened

qian
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 16 '23 at 14:19