有一个 mysql cpp connector 的小例子,在 mac 下编译命令:
g++ -o test_install -I/usr/local/include -I/usr/local/include/cppconn -Wl, -lmysqlcppconn test_mysql.cpp
编译完后生成 test_install 可以正常运行,连接到数据库并执行。
如何写出一个 CMakeLists.txt 有同样效果? 我的 CMakeLists.txt 如下:
cmake_minimum_required(VERSION 3.6)
project(TestCPP)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES
test_mysql.cpp)
add_executable(TestCPP ${SOURCE_FILES})
但是编译报错:
Undefined symbols for architecture x86_64:
"sql::mysql::get_driver_instance()", referenced from:
_main in test_mysql.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [TestCPP] Error 1
make[1]: *** [CMakeFiles/TestCPP.dir/all] Error 2
make: *** [all] Error 2
如何修改 CMakeLists.txt 使编译正确通过?
1
wsy2220 2016-09-25 19:49:39 +08:00 via Android
简单的办法是加上这两行:
``` include_directories(/usr/local/include /usr/local/include/cppconn) set(CMAKE_EXE_LINKER_FLAGS -lmysqlcppconn) ``` 更好的方法是用 find_package 或 find_library |