Why simple mysql++ code compiles standalone, but not in project?

独自空忆成欢 提交于 2019-11-28 12:28:24

问题


At first, small program:

#include <mysql++.h>
using namespace mysqlpp;

void mainuu ()
{ Connection conn("mysql", "localhost", "root", "pwd");}

If I compile it as one file in CodeLite or in such way:

g++ -I/usr/include/mysql -I/usr/include/mysql++ -lmysqlclient -lmysqlpp -o Test mysql_api.cpp

it's ok but, when I try to build whole project with this file I get this:

g++ -o ./Debug/server ./Debug/main.o ./Debug/log.o ./Debug/packet.o ./Debug/mysql_api.o  -L.   
./Debug/mysql_api.o: In function `mainuu()':
/home/asyler/.codelite/workspace/test/server/mysql_api.cpp:10: undefined reference to `mysqlpp::Connection::Connection(char const*, char const*, char const*, char const*, unsigned int)'
/home/asyler/.codelite/workspace/test/server/mysql_api.cpp:12: undefined reference to `mysqlpp::Connection::query(char const*)'
/home/asyler/.codelite/workspace/test/server/mysql_api.cpp:13: undefined reference to `mysqlpp::SQLTypeAdapter::SQLTypeAdapter(char const*, bool)'
/home/asyler/.codelite/workspace/test/server/mysql_api.cpp:13: undefined reference to `mysqlpp::operator<<(mysqlpp::quote_type1, mysqlpp::SQLTypeAdapter const&)'
/home/asyler/.codelite/workspace/test/server/mysql_api.cpp:19: undefined reference to `mysqlpp::operator<<(std::basic_ostream<char, std::char_traits<char> >&, mysqlpp::String const&)'
/home/asyler/.codelite/workspace/test/server/mysql_api.cpp:10: undefined reference to `mysqlpp::Connection::~Connection()'
/home/asyler/.codelite/workspace/test/server/mysql_api.cpp:10: undefined reference to `mysqlpp::Connection::~Connection()'
./Debug/mysql_api.o: In function `mysqlpp::Row::operator[](int) const':
/usr/include/mysql++/row.h:328: undefined reference to `mysqlpp::Row::at(unsigned int) const'
./Debug/mysql_api.o: In function `mysqlpp::Query::store()':
/usr/include/mysql++/query.h:467: undefined reference to `mysqlpp::Query::str(mysqlpp::SQLQueryParms&)'
/usr/include/mysql++/query.h:467: undefined reference to `mysqlpp::SQLTypeAdapter::SQLTypeAdapter(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool)'
/usr/include/mysql++/query.h:467: undefined reference to `mysqlpp::Query::store(mysqlpp::SQLTypeAdapter const&)'
collect2: ld returned 1 exit status
make[1]: *** [Debug/server] Error 1
make[1]: Leaving directory `/home/asyler/.codelite/workspace/test/server'
make: *** [All] Error 2

Here is CodeLite g++ compiler settings:

-g -I/usr/include/mysql -I/usr/include/mysql++ -lmysqlclient -lmysqlpp -L/usr/lib/mysql -L/usr/lib/mysql++ -lmysql++

回答1:


Looks like you need to edit CodeLite project settings and add these settings -lmysqlclient -lmysqlpp that you pass in command line. Fill Library Path and Libraries fields on Linker tab.




回答2:


Those are linker errors.

When you create your final executable, you still must provide references to all library functions, just as you did when you compiled the single translation unit.

So, pass -lmysqlclient -lmysqlpp to g++ this time, too.

If you are using an Integrated Development Environment, configure your project's build settings accordingly. In particular, I see that CodeLite has both "Compiler" and "Linker" build settings. It's "Linker" settings that you're after.

For more information on the build process (i.e. compiling, linking and the difference), read a good C++ book.



来源:https://stackoverflow.com/questions/7108740/why-simple-mysql-code-compiles-standalone-but-not-in-project

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!