0

我正在尝试使用 libwebsocket 来构建我的 WebSocket 服务器,我需要在我的代码中使用 libmysql。我已经安装了 libmysql 并且能够使用它,但我不知道如何在 CMakeList.txt 中使用 cmake 将它包含在内

我已将其包含在我的 test-server.h 中

#include <mysql.h>

在我的 CMakeList.txt 中,我添加了这个

INCLUDE_DIRECTORIES(/usr/include/mysql)//this is add to the begining

当我使用“make”来制作文件时,我收到了错误消息

CMakeFiles/test-server.dir/test-server/test-server.c.o: in function main:
test-server.c:(.text.startup+0x41): undefined reference to `mysql_init'
collect2: error: ld returned 1 exit status

这是 CMakeList.txt 的原始代码 https://github.com/warmcat/libwebsockets/blob/master/CMakeLists.txt

CMakeList.txt 由 libwebsocket 提供,我尝试使用以下代码对其进行编辑:

INCLUDE_DIRECTORIES(/usr/include/mysql)

Name: libwebsockets
Description: Websockets server and client library
Version: ${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACK    AGE_VERSION_PATCH}
Libs: -L/usr/lib/x86_64-linux-gnu -lmysqlclient -lpthread -lz -lm -ldl -L\${libdir} -lwebsockets
Cflags: -I/usr/include/mysql -DBIG_JOINS=1  -fno-strict-aliasing    -g -DNDEBUG -I\${includedir}"

为什么我不能链接图书馆?为了在我的 libwebsocket 服务器代码中使用 libmysql,我应该怎么做?我正在使用 ubuntu 14.04 作为操作系统。

4

1 回答 1

0

我建议您创建自己的 CMakeLists.txt 文件。

它应该处理您自己的代码,因为test-server.c只是一个示例,可让您了解如何构建自己的服务器。在我看来,它不打算被修改。但是,当然,如果您认为这样做更容易,则由您决定。

所以,现在,如果你真的想修改 libwebsockets 附带的示例,并且想保留关联的 CMakeLists.txt 文件,那么你可以修改create_test_app宏并修改对target_link_libraries的调用(除了你已经对包括路径)。

例如,您可以更改:

target_link_libraries(${TEST_NAME} websockets)

经过

target_link_libraries(${TEST_NAME} websockets mysqlclient)

最好利用FindMysql.cmake自动找出正确的包含路径和库位置。

于 2016-10-01T11:01:22.083 回答