SRombauts/SQLiteCpp

undefined reference to `__stack_chk_fail'

camielverdult opened this issue · 3 comments

This still seems to happen for the winlibs mingw compiler (GCC 12.2.0 + LLVM/Clang/LLD/LLDB 14.0.6 + MinGW-w64 10.0.0 (UCRT) - release 2)

cmd.exe /C "cd . && C:\Users\cverdult\mingw64\bin\c++.exe -g  CMakeFiles/cheese.dir/main.cpp.obj CMakeFiles/cheese.dir/src/dataset.cpp.obj CMakeFiles/cheese.dir/src/cheese_maker.cpp.obj CMakeFiles/cheese.dir/src/helpers.cpp.obj CMakeFiles/cheese.dir/src/cheese_eater.cpp.obj -o cheese.exe -Wl,--out-implib,libcheese.dll.a -Wl,--major-image-version,0,--minor-image-version,0  _deps/sqlitecpp-build/libSQLiteCpp.a  _deps/fmt-build/libfmtd.a  _deps/csv-build/include/internal/libcsv.a  --coverage  _deps/sqlitecpp-build/sqlite3/libsqlite3.a  -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
c:/users/cverdult/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: _deps/sqlitecpp-build/libSQLiteCpp.a(Database.cpp.obj): in function `SQLite::Database::execAndGet(char const*)':
C:/Users/cverdult/Documents/cheese/cmake-build-debug/_deps/sqlitecpp-src/src/Database.cpp:129: undefined reference to `__stack_chk_fail'
c:/users/cverdult/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: _deps/sqlitecpp-build/libSQLiteCpp.a(Database.cpp.obj): in function `SQLite::Database::tableExists(char const*) const':
C:/Users/cverdult/Documents/cheese/cmake-build-debug/_deps/sqlitecpp-src/src/Database.cpp:138: undefined reference to `__stack_chk_fail'
c:/users/cverdult/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: _deps/sqlitecpp-build/libSQLiteCpp.a(Database.cpp.obj): in function `SQLite::Database::isUnencrypted(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
C:/Users/cverdult/Documents/cheese/cmake-build-debug/_deps/sqlitecpp-src/src/Database.cpp:287: undefined reference to `__stack_chk_fail'
c:/users/cverdult/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: _deps/sqlitecpp-build/libSQLiteCpp.a(Database.cpp.obj): in function `SQLite::Database::getHeaderInfo(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
C:/Users/cverdult/Documents/cheese/cmake-build-debug/_deps/sqlitecpp-src/src/Database.cpp:421: undefined reference to `__stack_chk_fail'
c:/users/cverdult/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: _deps/sqlitecpp-build/libSQLiteCpp.a(Database.cpp.obj): in function `SQLite::Database::backup(char const*, SQLite::Database::BackupType)':
C:/Users/cverdult/Documents/cheese/cmake-build-debug/_deps/sqlitecpp-src/src/Database.cpp:437: undefined reference to `__stack_chk_fail'
c:/users/cverdult/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: _deps/sqlitecpp-build/libSQLiteCpp.a(Database.cpp.obj):Database.cpp:(.rdata$.refptr.__stack_chk_guard[.refptr.__stack_chk_guard]+0x0): undefined reference to `__stack_chk_guard'
c:/users/cverdult/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: _deps/sqlitecpp-build/libSQLiteCpp.a(Statement.cpp.obj): in function `SQLite::Statement::getColumnIndex(char const*) const':
C:/Users/cverdult/Documents/cheese/cmake-build-debug/_deps/sqlitecpp-src/src/Statement.cpp:289: undefined reference to `__stack_chk_fail'
c:/users/cverdult/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: _deps/sqlitecpp-build/sqlite3/libsqlite3.a(sqlite3.c.obj): in function `datetimeFunc':
C:/Users/cverdult/Documents/cheese/cmake-build-debug/_deps/sqlitecpp-src/sqlite3/sqlite3.c:24317: undefined reference to `__stack_chk_fail'
c:/users/cverdult/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: _deps/sqlitecpp-build/sqlite3/libsqlite3.a(sqlite3.c.obj): in function `timeFunc':
C:/Users/cverdult/Documents/cheese/cmake-build-debug/_deps/sqlitecpp-src/sqlite3/sqlite3.c:24346: undefined reference to `__stack_chk_fail'
c:/users/cverdult/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: _deps/sqlitecpp-build/sqlite3/libsqlite3.a(sqlite3.c.obj): in function `dateFunc':
C:/Users/cverdult/Documents/cheese/cmake-build-debug/_deps/sqlitecpp-src/sqlite3/sqlite3.c:24383: undefined reference to `__stack_chk_fail'
c:/users/cverdult/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: _deps/sqlitecpp-build/sqlite3/libsqlite3.a(sqlite3.c.obj): in function `sqlite3_str_vappendf':
C:/Users/cverdult/Documents/cheese/cmake-build-debug/_deps/sqlitecpp-src/sqlite3/sqlite3.c:30563: undefined reference to `__stack_chk_fail'
c:/users/cverdult/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: _deps/sqlitecpp-build/sqlite3/libsqlite3.a(sqlite3.c.obj):C:/Users/cverdult/Documents/cheese/cmake-build-debug/_deps/sqlitecpp-src/sqlite3/sqlite3.c:30861: more undefined references to `__stack_chk_fail' follow

Sqlite is downloaded and linked as following:

FetchContent_Declare(
        SQLiteCpp
        GIT_REPOSITORY      https://github.com/SRombauts/SQLiteCpp.git
        GIT_TAG             master
)

FetchContent_MakeAvailable(SQLiteCpp)

add_executable(
        cheese
        main.cpp
        src/dataset.cpp
        src/cheese_maker.cpp
        src/helpers.cpp
        src/cheese_eater.cpp

        include/constants.hpp
        include/dataset.hpp
        include/cheese_maker.hpp
        include/helpers.hpp
        include/cheese_eater.hpp
)

target_link_libraries(
        cheese 
        PRIVATE
        SQLiteCpp
)

target_compile_features(cheese PRIVATE cxx_std_20)

This is very similiar to #74

Yes, the issue is that we enable stack protection by default, which is not supported on Windows.
You have to explicitly disable is by setting SQLITECPP_USE_STACK_PROTECTION to OFF;
eg -DSQLITECPP_USE_STACK_PROTECTION=OFF on the CMake command line

A better way would be to improve the CMakeLists.txt to detect the MinGW compiler and disable stack protection automatically instead of relying on the user to set it to false.

# Stack protection is not supported on MinGW-W64 on Windows, allow this flag to be turned off.
option(SQLITECPP_USE_STACK_PROTECTION "USE Stack Protection hardening." ON)
if (SQLITECPP_USE_STACK_PROTECTION)
    message (STATUS "Using Stack Protection hardening")
    add_compile_options(-fstack-protector)
endif()

I didn't want to do that myself as I don't use MinGW and sadly MinGW is not tested on CI/CD so I am not too confident in making changes that are not tested.
I'll try to add MinGW to GitHub Actions

Let me know if it works as expected for you, after cleaning and re-generating the cmake project

gvanem commented

I hit this same link.exe error when building Cairo with meson:
meson .. --backend vs2022 --buildtype=release

I noted to my amazement that Meson added a -If:/Cygwin64/include that possibly pulled in this symbol.
Can someone please help with this?