0

I am trying to use libwebsocket to build up my WebSocket server and I need to used libmysql in my code. I have install the libmysql and able to use it but I don't know how to include it using cmake in CMakeList.txt

I have include it in my test-server.h

#include <mysql.h>

In my CMakeList.txt, I add this

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

when I use "make" to make the file, I got the error message

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

Here is the original code of the CMakeList.txt https://github.com/warmcat/libwebsockets/blob/master/CMakeLists.txt

The CMakeList.txt is provided by libwebsocket and I try to edit it with the follow code:

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}"

Why I can not link the library? What should I do in order to use libmysql in my libwebsocket server code? I am using ubuntu 14.04 as OS.

kennyfung
  • 1
  • 1
  • You cmake file is more than 1600 lines. Please make a [MCVE](https://www.google.fr/url?sa=t&rct=j&q=&esrc=s&source=web&cd=7&cad=rja&uact=8&ved=0ahUKEwjWmaqFn6fOAhXKBBoKHcVhAt8QFghNMAY&url=http%3A%2F%2Fstackoverflow.com%2Fhelp%2Fmcve&usg=AFQjCNEkKReghNZHBJSaky_hwPskMmG0ww&sig2=FB_BPk5I8OUGCqxfz0MbSA) and post its content in your question, not through an external link – rocambille Aug 04 '16 at 07:32
  • I try to quote the code that I have add to the CMakeList.txt. I edit the Libs and Cflags but it is still not ok... – kennyfung Aug 05 '16 at 02:27

1 Answers1

0

I would recommend you to create your own CMakeLists.txt file.

It should takes care of your own code, because test-server.c is just an example that allow you to understand how to build your own server. In my opinion, it is not intend to be modified. But, of course, it is up to you to do so if you think it is easier.

So, now, if you really want to modify the example coming with libwebsockets, and want to keep the associated CMakeLists.txt file, then you could modify the create_test_app macro and modify calls to target_link_libraries (in addition to the modification you have already made regarding include path).

For example you could change:

target_link_libraries(${TEST_NAME} websockets)

by

target_link_libraries(${TEST_NAME} websockets mysqlclient)

It should be even better to take advantage of FindMysql.cmake to automatically find out what are the correct include paths and libraries locations.

Pierre
  • 134
  • 9