2

Estou montando um projeto scaffold para cross compile em c/c++ para Windows, Linux, Mac, Ios e Android. Quando compilo o projeto para Windows x86 e tenho um erro muito estranho, porem o mesmo código compila com sucesso para Windows x64. Quando eu removo a utilização do vector consigo compilar para Windows x86. Gostaria de saber se está faltando alguma biblioteca para link?

Segue link do repositório. 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'

Estou utilizando esses pacotes no 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})

Comando no 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
  • 1
    Recomendo usar CMAKE_TOOLCHAIN_FILE ao invés de ficar criando condições (if) dentro do CMakeLists.txt para tratar diferentes sistemas operacionais, arquiteturas e compiladores. Seu script de build seria muito mais simples nesse caso. Commented 10/03/2020 às 17:31

1 Resposta 1

1

Por alguma razão o Clang não está encontrando os símbolos utilizados para desenrolar a pilha (stack unwinding), que provavelmente está em uma biblioteca específica. Por conta disso, se você adicionar a flag -fno-exceptions o erro some:

$ 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")
$ 

É claro que ao fazer isso você não poderá mais usar exceções.

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .