undefined reference to `get_driver_instance'



  • When an attempt is made to launch a project in the clion environment (JetBrains), I get a misstatement report (see below). But if you're compiling on the command line:

    ~/MyProg$ g++ -o test main.cpp -I/usr/include/cppconn -L/usr/lib -lmysqlcppconn -std=c++11  
    

    It works.
    What am I doing wrong? What could be the reason for the error?

    Erroneous report:

    /home/user/clion-1.2.4/bin/cmake/bin/cmake --build
    /home/user/.CLion12/system/cmake/generated/5c44c2f9/5c44c2f9/Debug --target MyProg --j 4
    [66%] Building CXX object CMakeFiles/MyProg.dir/main.cpp.o
    [100%] Linking CXX executable MyProg
    CMakeFiles/MyProg.dir/main.cpp.o: In function `getDraws()':
    /home/user/ClionProjects/MyProg/draws.h:28: undefined reference to `get_driver_instance '
    collect2: error: ld returned 1 exit status
    make[3]: *** [MyProg] Error 1
    make[2]: *** [CMakeFiles/MyProg.dir/all] Mistake 2
    make[1]: *** [CMakeFiles/MyProg.dir/rule] Mistake 2
    make: ***

    Compilable code:

    
    #include "mysql_connection.h"
    #include <cppconn/driver.h>
    #include <cppconn/exception.h>
    #include <cppconn/resultset.h>
    #include <cppconn/statement.h>
    #include <vector>
    

    using namespace std;
    using namespace sql;

    vector<vector<int> getDraws()
    {
    try
    {
    Driver *driver;
    Connection *con;
    Statement *stmt;
    ResultSet *res;

        /* Create a connection */
        driver = get_driver_instance(); // &lt;--- ВОТ ТУТ
        con = driver-&gt;connect("tcp://127.0.0.1:3306", "root", "pass");
        /* Connect to the MySQL test database */
        con-&gt;setSchema("schema");
    
        stmt = con-&gt;createStatement();
        res = stmt-&gt;executeQuery("SELECT * from table");
    
        int drawsCount = res-&gt;rowsCount();
        vector&lt;vector&lt;int&gt; &gt; draws(drawsCount, vector&lt;int&gt;(2, 0));
    
        while (res-&gt;next())
        {
            int i = res-&gt;getInt("draw") - 1;
            draws[i][0] = res-&gt;getInt("col0");
            draws[i][1] = res-&gt;getInt("col1");
        }
    
        delete res;
        delete stmt;
        delete con;
    
        return draws;
    }
    catch (SQLException &amp;e)
    {
       ...
    

    }

    Cmake file:


    cmake_minimum_required(VERSION 3.3)
    project(MyProg)

    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -I/usr/include/cppconn -L/usr/lib -lmysqlcppconn")

    set(SOURCE_FILES main.cpp)
    add_executable(MyProg ${SOURCE_FILES})



  • Separate compilation and lingo guidelines should be used:

    cmake_minimum_required(VERSION 3.3)
    project(MyProg)
    

    add_definitions(-std=c++11)
    include_directories(/usr/include/cppconn)
    link_directories(/usr/lib)

    set(SOURCE_FILES main.cpp)
    add_executable(MyProg ${SOURCE_FILES})
    target_link_libraries(MyProg mysqlcppconn)

    Reference /usr/include and /usr/lib Maybe not necessarily.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2