有一个 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 使编译正确通过?
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.