Reference not defined in compiling an executable for windows x86

Asked

Viewed 146 times

2

I’m building a scaffold project for cross-Compile on c/c++ for Windows, Linux, Mac, Ios and Android. When I compile the project for Windows x86 and I have a very strange error, but the same code successfully compiles to Windows x64. When I removed the use of vector I can compile for Windows x86. I wonder if any library is missing for link?

Following link from the repository. https://github.com/Jhonnycpp/Jia

CMakeFiles/jia.dir/src/main.cpp.o:main.cpp:(.text+0x1c2): undefined reference to `_Unwind_Resume'
Info: resolving __ZSt4cout by linking to __imp___ZSt4cout (auto-import)
CMakeFiles/jia.dir/src/main.cpp.o:main.cpp:(.text$_ZNSt6vectorIiSaIiEE17_M_realloc_insertIJiEEEvN9__gnu_cxx17__normal_iteratorIPiS1_EEDpOT_[__ZNSt6vectorIiSaIiEE17_M_realloc_insertIJiEEEvN9__gnu_cxx17__normal_iteratorIPiS1_EEDpOT_]+0x27b): undefined reference to `_Unwind_Resume'
CMakeFiles/jia.dir/src/main.cpp.o:main.cpp:(.eh_frame+0x2ff): undefined reference to `__gxx_personality_v0'

I am using these packages in Ubuntu build-Essential, gcc-multilib, g++-multilib, Clang-9, cmake, mingw-W64.

//main.cpp
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int> t;
    t.push_back(1);
    t.push_back(2);
    t.push_back(2);

    for(int it : t)
        cout << it << endl;

    cout << "Hello word" << endl << endl;
}

# cmakelists.txt
# CMakeLists files in this project can
# refer to the root source directory of the project as ${HELLO_SOURCE_DIR} and
# to the root binary directory of the project as ${HELLO_BINARY_DIR}.
cmake_minimum_required (VERSION 3.10)
# set(CMAKE_C_COMPILER "clang" CACHE PATH "Path llvm" FORCE)
set(CMAKE_CXX_COMPILER "clang++-9" CACHE PATH "Path llvm" FORCE)

project(jia CXX)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(NOT DEFINED OS)
    message(FATAL_ERROR  "Operation system not defined.")
endif()

if(NOT DEFINED ARCH)
    message(FATAL_ERROR  "Architecture not defined.")
endif()

set(WORKSPACE ${PROJECT_SOURCE_DIR})
set(CMAKE_BINARY_DIR ${WORKSPACE}/build)

string(TOUPPER ${OS} ${OS})

SET(CUSTOM_BUILD_PARAMS "")
SET(CUSTOM_LINKER_PARAMS "")

if(${OS} STREQUAL "WINDOWS")
    set(CMAKE_SYSTEM_NAME Windows)
    set(CMAKE_CROSSCOMPILING TRUE)

    set(MINGW_ARCH ${ARCH}-w64-mingw32)
    set(TARGET ${ARCH}-w64-mingw32-gcc)

    set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
    set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
    set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

    SET(CUSTOM_BUILD_PARAMS "${CUSTOM_BUILD_PARAMS} -DWIN32")
    SET(CUSTOM_BUILD_PARAMS "${CUSTOM_BUILD_PARAMS} -D_UNICODE")
    SET(CUSTOM_BUILD_PARAMS "${CUSTOM_BUILD_PARAMS} -DUNICODE")
    SET(CUSTOM_BUILD_PARAMS "${CUSTOM_BUILD_PARAMS} -D__GNUC__=4") # -mwindows

    set (CMAKE_EXECUTABLE_SUFFIX ".exe")

    set(CMAKE_POSITION_INDEPENDENT_CODE OFF)
    set(OUTTARGET ${ARCH}-windows)
endif()

SET(CUSTOM_BUILD_PARAMS "${CUSTOM_BUILD_PARAMS} -target ${TARGET}")

SET(CMAKE_C_FLAGS  "${CMAKE_CXX_FLAGS} ${CUSTOM_BUILD_PARAMS}" CACHE STRING "toolchain_cflags" FORCE)
SET(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} ${CUSTOM_BUILD_PARAMS}" CACHE STRING "toolchain_cflags" FORCE)
SET(CMAKE_EXE_LINKER_FLAGS  "${CMAKE_EXE_LINKER_FLAGS} ${CUSTOM_LINKER_PARAMS} -v" CACHE STRING "toolchain_cflags" FORCE)

SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_HOME_DIRECTORY}/build/${OUTTARGET})
SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_HOME_DIRECTORY}/build/${OUTTARGET})
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_HOME_DIRECTORY}/build/${OUTTARGET})

set(SOURCES
    ${WORKSPACE}/src/main.cpp
)

# set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

add_executable (jia ${SOURCES})

target_link_libraries(jia PRIVATE ${LINK_LIBRARIES})

Command on the Linker

x86

"/usr/bin/ld" -m i386pe -Bdynamic -o build/i686-windows/jia.exe /usr/i686-w64-mingw32/lib/crt2.o /usr/lib/gcc/i686-w64-mingw32/7.3-win32/crtbegin.o -L/usr/lib/gcc/i686-w64-mingw32/7.3-win32 -L/usr/i686-w64-mingw32/lib -L/usr/lib -L/usr/i686-w64-mingw32/sys-root/mingw/lib CMakeFiles/jia.dir/src/neuron.cpp.o CMakeFiles/jia.dir/src/main.cpp.o -lstdc++ -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt /usr/lib/gcc/i686-w64-mingw32/7.3-win32/crtend.o

x64

"/usr/bin/ld" -m i386pep -Bdynamic -o build/x86_64-windows/jia.exe /usr/x86_64-w64-mingw32/lib/crt2.o /usr/lib/gcc/x86_64-w64-mingw32/7.3-win32/crtbegin.o -L/usr/lib/gcc/x86_64-w64-mingw32/7.3-win32 -L/usr/x86_64-w64-mingw32/lib -L/usr/lib -L/usr/x86_64-w64-mingw32/sys-root/mingw/lib CMakeFiles/jia.dir/src/neuron.cpp.o CMakeFiles/jia.dir/src/main.cpp.o -lstdc++ -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt /usr/lib/gcc/x86_64-w64-mingw32/7.3-win32/crtend.o
  • 1

    I recommend using CMAKE_TOOLCHAIN_FILE instead of creating conditions (if) within the CMakeLists.txt to handle different operating systems, architectures and compilers. Your build script would be much simpler in this case.

1 answer

1


For some reason Clang is not finding the symbols used to unwind the stack (unwinding stack), which is probably in a specific library. Because of this, if you add the flag -fno-exceptions the error adds up:

$ git diff
diff --git a/CMakeLists.txt b/CMakeLists.txt
index c6eb183..f161adc 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -24,7 +24,7 @@ set(CMAKE_BINARY_DIR ${WORKSPACE}/build)

 string(TOUPPER ${OS} ${OS})

-SET(CUSTOM_BUILD_PARAMS "")
+SET(CUSTOM_BUILD_PARAMS "-fno-exceptions")
 SET(CUSTOM_LINKER_PARAMS "")

 if(${OS} STREQUAL "WINDOWS")
$ 

Of course, by doing this you can no longer use exceptions.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.