c++ - mysql C++ 不会用 make 编译,但如果 g++ 参数放在 cmd 行

标签 c++ mysql c++11 makefile g++

所以我只是想编译这个示例 mysql C++ 程序。如果我将 g++ -Wall -I/usr/include/cppconn -o mysql_test mysql_test.cpp -L/usr/lib -lmysqlcppconn 放在命令行上,它将编译并运行得很好。如果我尝试使用 make,它会由于未定义的函数调用而失败。 make 脚本有什么问题?

g++ -o prog mysql_test.o mysql_test.o: In function main': mysql_test.cpp:(.text+0x3a): undefined reference toget_driver_instance' collect2: error: ld returned 1 exit status make: *** [all] Error 1

这是生成文件:

CXX=g++
#CXXFLAGS=-g -std=c++0x -Wall -pedantic -lmysqlcppconn -lmysqlcppconn-static
CXXFLAGS=-g -std=c++0x -Wall -I/usr/include/cppconn -L/usr/lib -lmysqlcppconn
BIN=prog

SRC=$(wildcard *.cpp)
OBJ=$(SRC:%.cpp=%.o)

all: $(OBJ)
    $(CXX) -o $(BIN) $^

%.o: %.c
    $(CXX) $@ -c $<

clean:
    rm -f *.o
    rm $(BIN)

这是 cpp 源代码:

#include <stdlib.h>
#include <iostream>

/*
  Include directly the different
  headers from cppconn/ and mysql_driver.h + mysql_util.h
  (and mysql_connection.h). This will reduce your build time!
*/
#include <mysql_connection.h>

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


using namespace std;

int main(void)
{
cout << endl;
cout << "Running 'SELECT 'Hello World!' AS _message'..." << endl;

try {
  sql::Driver *driver;
  sql::Connection *con;
  sql::Statement *stmt;
  sql::ResultSet *res;

  /* Create a connection */
  driver = get_driver_instance();
  con = driver->connect("tcp://127.0.0.1:3306", "root", "password");
  /* Connect to the MySQL test database */
  con->setSchema("mysql");

  stmt = con->createStatement();
  res = stmt->executeQuery("SELECT 'Hello World!' AS _message");
  while (res->next()) {
    cout << "\t... MySQL replies: ";
    /* Access column data by alias or column name */
    cout << res->getString("_message") << endl;
    cout << "\t... MySQL says it again: ";
    /* Access column fata by numeric offset, 1 is the first column */
    cout << res->getString(1) << endl;
  }
  delete res;
  delete stmt;
  delete con;

} 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;
}

cout << endl;

return EXIT_SUCCESS;
}

感谢您对此的任何帮助。

这是工作品牌:

CXX=g++
#CXXFLAGS=-g -std=c++0x -Wall -pedantic -lmysqlcppconn -lmysqlcppconn-static
CXXFLAGS=-g -std=c++0x -Wall -I/usr/include/cppconn
LDFLAGS=-L/usr/lib -lmysqlcppconn
BIN=prog

SRC=$(wildcard *.cpp)
OBJ=$(SRC:%.cpp=%.o)

all: $(OBJ)
    $(CXX) -o $(BIN) $(LDFLAGS) $^

%.o: %.c
    $(CXX) $@ -c $<

clean:
    rm -f *.o
    rm $(BIN) 

最佳答案

-L/usr/lib -lmysqlcppconn 需要在 LD_FLAGS 中。

关于c++ - mysql C++ 不会用 make 编译,但如果 g++ 参数放在 cmd 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29877898/

相关文章:

mysql - 将 BIGINT UNSIGNED 转换为 INT

c++ - decltype 在模板参数推导失败的地方成功?

c++ - GCC 严格溢出

php - 依赖mysql中的错误代码1062

mysql - 这个矩阵乘法在 SQL 中是如何工作的?

c++ - 无法将 google::sparse_hash_map 的值编译为 unique_ptr

c++ - OS X libc++ std::uniform_real_distribution 错误

c++ - 应用程序崩溃时如何刷新ostream

C++ 静态库

c++ - 从 C++ 中的函数返回用户定义对象列表时出错