0

Here is my Common library CMakeLists.txt file:

add_library(
    Common
        SHARED
        Config.cpp
        Config.h
        Logger.cpp
        Logger.h
        Database/DatabaseLoader.cpp
        Database/DatabaseLoader.h
        Database/MySQLConnection.h
        Database/MySQLConnection.cpp
        Database/MySQLTable.h
        Database/MySQLTable.cpp
    )
if(WIN32)

    # Add the MySQL include directories to this target.
    target_include_directories(Common PUBLIC
            ${FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR}/include/jdbc
            )
    target_include_directories(Common PUBLIC
            ${FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR}/include/jdbc/cppconn
            )

    # Link the MySQL library to your executable.
    target_link_libraries(Common PUBLIC
            ${FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR}/lib64/vs14/mysqlcppconn8.lib
            )
else()
target_link_libraries(Common LINK_PUBLIC ${MYSQL_LIBRARY} mysqlcppconn)
endif()
target_include_directories(Common PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

Here is the function which triggers the error:

void MySQLConnection::Connect(MySQLConnectionInfo &mySqlConnectionInfo) {
    const sql::SQLString hostname = "tcp://" + mySqlConnectionInfo.host + ":" + mySqlConnectionInfo.mysqlPort;

    try {
        //* Create a connection *//*
        _driver = get_driver_instance();
        _mySQLConn = _driver->connect(hostname, mySqlConnectionInfo.mysqlUser, mySqlConnectionInfo.mysqlPassword);
        //* Connect to the MySQL test database *//*
        _mySQLConn->setSchema(mySqlConnectionInfo.mysqlDatabase);

    } catch (sql::SQLException &e) {
        cout << "# ERR: SQLException in " << __FILE__;
        cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
        cout << "# ERR: " << e.what();
        cout << " (MySQL error code: " << e.getErrorCode();
        cout << ", SQLState: " << e.getSQLState() << " )" << endl;
    }
}

When I try to build I get the following error:

[100%] Linking CXX shared library ..\..\bin\Common.dll
NMAKE : fatal error U1077: '"C:\Program Files\JetBrains\CLion 2020.1.3\bin\cmake\win\bin\cmake.exe"' : return code '0xffffffff'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
LINK Pass 1: command "C:\PROGRA~2\MICROS~1\2019\COMMUN~1\VC\Tools\MSVC\1424~1.283\bin\Hostx64\x64\link.exe /nologo @CMakeFiles\Common.dir\objects1.rsp /out:..\..\bin\Common.dll /implib:..\..\bin\lib\Common.lib /pdb:E:\Vibranium-Core\cmake-build-debug\bin\Common.pdb /dll /version:0.0 /machine:x64 /debug /INCREMENTAL C:\Program Files\MySQL\Connector C++ 8.0\lib64\vs14\mysqlcppconn8.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\Common.dir/intermediate.manifest CMakeFiles\Common.dir/manifest.res" failed (exit code 1120) with the following output:
MySQLConnection.cpp.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl sql::SQLString::~SQLString(void)" (__imp_??1SQLString@sql@@QEAA@XZ) referenced in function "private: void __cdecl Vibranium::MySQLConnection::Connect(struct Vibranium::MySQLConnectionInfo &)" (?Connect@MySQLConnection@Vibranium@@AEAAXAEAUMySQLConnectionInfo@2@@Z)
MySQLTable.cpp.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: __cdecl sql::SQLString::~SQLString(void)" (__imp_??1SQLString@sql@@QEAA@XZ)
MySQLConnection.cpp.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl sql::SQLString::SQLString(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (__imp_??0SQLString@sql@@QEAA@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "private: void __cdecl Vibranium::MySQLConnection::Connect(struct Vibranium::MySQLConnectionInfo &)" (?Connect@MySQLConnection@Vibranium@@AEAAXAEAUMySQLConnectionInfo@2@@Z)
MySQLTable.cpp.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: __cdecl sql::SQLString::SQLString(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (__imp_??0SQLString@sql@@QEAA@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
MySQLConnection.cpp.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const & __cdecl sql::SQLException::getSQLState(void)const " (__imp_?getSQLState@SQLException@sql@@QEBAAEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function __catch$?Connect@MySQLConnection@Vibranium@@AEAAXAEAUMySQLConnectionInfo@2@@Z$0
MySQLTable.cpp.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const & __cdecl sql::SQLException::getSQLState(void)const " (__imp_?getSQLState@SQLException@sql@@QEBAAEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ)
MySQLConnection.cpp.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: int __cdecl sql::SQLException::getErrorCode(void)const " (__imp_?getErrorCode@SQLException@sql@@QEBAHXZ) referenced in function __catch$?Connect@MySQLConnection@Vibranium@@AEAAXAEAUMySQLConnectionInfo@2@@Z$0
MySQLTable.cpp.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: int __cdecl sql::SQLException::getErrorCode(void)const " (__imp_?getErrorCode@SQLException@sql@@QEBAHXZ)
MySQLConnection.cpp.obj : error LNK2019: unresolved external symbol __imp_get_driver_instance referenced in function "private: void __cdecl Vibranium::MySQLConnection::Connect(struct Vibranium::MySQLConnectionInfo &)" (?Connect@MySQLConnection@Vibranium@@AEAAXAEAUMySQLConnectionInfo@2@@Z)
..\..\bin\Common.dll : fatal error LNK1120: 5 unresolved externals

Note that I receive this build error only on Windows. On Linux it builds fine. By my understanding I have a problem with the mysql connector. I have installed the connector and I have the files. Take a look:

enter image description here

Where is my mistake and how can I solve it?

Venelin
  • 2,905
  • 7
  • 53
  • 117
  • Looks like you are not linking to the import library needed for MySQL connector. Are you building a 32 or 64 bit application? – drescherjm Aug 28 '20 at 13:36
  • Try putting the mysql connector in a path without spaces. Also, I'm not an expert but there are mysqlcppconn8.lib and mysqlcppconn.lib. Not sure which one you need. – rustyx Aug 28 '20 at 13:41
  • A amd64 bit app is the build target. – Venelin Aug 28 '20 at 13:41
  • There were no other errors above the message printed about can't open the library? – drescherjm Aug 28 '20 at 13:42
  • @drescherjm I have updated the question with the full error. Check it out please – Venelin Aug 28 '20 at 13:45
  • I see there are two libs mysqlcppconn8 and mysqlcppconn. In linux you link to mysqlcppconn but in windows mysqlcppconn8. I assume there is some way to activate one over the other?? I obviously don't use mysqlconnector – drescherjm Aug 28 '20 at 13:45
  • if I switch to `${FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR}/lib64/vs14/mysqlcppconn.lib` when I try to run the app i get `Process finished with exit code -1073741515 (0xC0000135)` – Venelin Aug 28 '20 at 13:52
  • 1
    That means it worked and you did not put the .dll files in a folder that is in the `PATH` environment variable or in the same location as the executable. Becareful if there are separate debug and release dlls. If there are make sure you use the debug ones in debug and release ones in release. – drescherjm Aug 28 '20 at 13:57
  • `0xC0000135` means dll not found here: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-erref/596a1078-e883-4972-9bbc-49e60bebca55 – drescherjm Aug 28 '20 at 14:05
  • @drescherjm which is the dll that it can't find ? – Venelin Aug 28 '20 at 17:19
  • I am afraid that is all the info that is given. The windows error is just a 32 bit code. Could be one of the dlls of mysqlconnector since you chose to link with an import library instead of the static library. Could be the dlls for your ssl or crypto, if these were built with gcc, it could be the gcc runtime. – drescherjm Aug 28 '20 at 17:19
  • @drescherjm how can I set it to import library ? – Venelin Aug 28 '20 at 21:32
  • Your question probably should have been I do I switch from using the import library that I do now (import library means dll is needed) to use the static library. I don't know it should be in the documentation. On windows there is typically a preprocessor symbol that controls this. – drescherjm Aug 28 '20 at 21:35
  • `__declspec(dllimport)` meant you promised to use the import library. A header should have had a macro that activated this. This is typically how it works: [https://stackoverflow.com/questions/14980649/macro-for-dllexport-dllimport-switch](https://stackoverflow.com/questions/14980649/macro-for-dllexport-dllimport-switch) – drescherjm Aug 28 '20 at 21:36
  • This was why you had the original error when you failed to link to the correct import library. – drescherjm Aug 28 '20 at 21:43
  • BTW, unrelated, but note that mysql connector is GPL, using it makes your program open-source. Mariadb connector is a possible alternative with a better license. – rustyx Aug 28 '20 at 23:02
  • @rustyx how can I link the mariadb connector on windows and linux? Which is the exact connector I need ? – Venelin Aug 29 '20 at 06:59
  • It will be roughly the same as mysql as mariadb is a fork. – drescherjm Aug 29 '20 at 15:22

1 Answers1

0

The linker didnt find the correct symbols for mysqlconnector library.

Either you didn't setup the library path correctly

or

you are using the libraries compiled with different options or with different compiler.

StPiere
  • 4,113
  • 15
  • 24