Cristiano Araujo
Cristiano Araujo

Reputation: 1962

Linker error with CMake

I'm using JetBrains CLion for pure C (C ANSI) development, I know it's target is C++, but my company works only with C and CLion uses only CMake as build system.

My system is a Debian Jessie system and sqlite3 and libsqlite3-dev are installed.

I'm trying to build a simple sqlite3 project like this:

#include <sqlite3.h>
#include <stdio.h>

    int main() {
    sqlite3 *sqlConnection;
    int ret;
    ret = sqlite3_open("database/path.db, &sqlConnection);
    if (ret) {
        printf("Ups ... can't open %d", ret);
    }
    do_some_queries(sqlConnection);
    return 0;
}

The automatic generated CMakeLists.txt is the follwing.

cmake_minimum_required(VERSION 3.3)


project(Project)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp )
add_executable(Project ${SOURCE_FILES})

When build, either through Clion, either through command line, I get linker errors:

...
undefined reference to `sqlite3_prepare_v2'
undefined reference to `sqlite3_column_int'
undefined reference to `sqlite3_open'
...

I know I must point out to CMake where sqlite3 is, but I can't find a way of doing this.

"find_package" and "find_library" may do it, but I can't find how.

I've also found this Cmake file, but could not used successfully.

So, how do I integrate sqlite3 with Cmake ?

Upvotes: 1

Views: 3227

Answers (2)

Cristiano Araujo
Cristiano Araujo

Reputation: 1962

I've founded a workaround, but it's not the correct way of doing.

Since on Debian the "gcc main.c -lsqlite3" works (with libsqlite3-dev installed), passing the -lsqlite3 flag to the linker do the trick.

so I've changed

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

to

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lsqlite3")

And it worked.

Upvotes: -1

neerad29
neerad29

Reputation: 473

You need to add the path to sqlite header file to your include path. Then link the sqlite library using target_link_libraries:

https://cmake.org/cmake/help/v3.3/command/target_link_libraries.html

Upvotes: 2

Related Questions