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

时间:2015-04-26 12:56:12

标签: 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:在函数main': mysql_test.cpp:(.text+0x3a): undefined reference to中get_driver_instance'   collect2:错误:ld返回1退出状态   make:*** [all]错误1

这是make文件:

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) 

1 个答案:

答案 0 :(得分:0)

-L/usr/lib -lmysqlcppconn需要位于LD_FLAGS