Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

These compiler options into a CMakeLists.txt

I am trying to create a CMakeLists.txt out of the inline compile options-

g++ -Wall -I/usr/include/cppconn -o testapp tester.cpp -L/usr/lib -lmysqlcppconn

I expected the following to work

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)

set(CMAKE_C_FLAGS "-Wall")

project(testapp)

find_package(PCL 1.4 REQUIRED)

include_directories(. "/usr/include/cppconn")

link_directories("/usr/lib/" "mysqlcppconn")

add_executable(testapp tester.cpp) 

But in make I get undefined reference errors for the above library's content. Am I misunderstanding include_directories and/or link_directories?

My tester.cpp includes are this

#include <stdlib.h>
#include <iostream>
#include <mysql_connection.h>
#include <driver.h>
#include <exception.h>
#include <resultset.h>
#include <statement.h>
like image 534
rocketas Avatar asked Feb 12 '23 07:02

rocketas


2 Answers

You also need to actually link against the library, link_directories() only specifies multiple directories. You need to add

target_link_libraries(testapp mysqlcppconn)

after add_executable()

also you should be able to skip the link_directories() completely /usr/lib is usually in the library search path by default.

like image 53
lazycoder Avatar answered Feb 14 '23 19:02

lazycoder


You aren't actually specifying the libraries you want to link to anywhere, see the documentation for link_directories (all it takes is a list of directories). You'll want to look into target_link_libraries. You may also want to look into find_library and "find modules" for the preferred way of handling 3rd party libraries with CMake (though you don't necessarily need to).

like image 39
Peter Clark Avatar answered Feb 14 '23 19:02

Peter Clark