1

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 ?

usr1234567
  • 21,601
  • 16
  • 108
  • 128
Cristiano Araujo
  • 1,632
  • 2
  • 21
  • 32
  • Add `sqlite3.c` to the sources. – CL. Jul 15 '16 at 17:34
  • @CL. you say download sqlite3 source code and compile it together with the project ? I've tried it and got other linker errors (threading and etc), moreover, don't like duplicating sources around the system. Will try tough. – Cristiano Araujo Jul 15 '16 at 17:38
  • Possible duplicate of [how to add linker or compile flag in cmake file](http://stackoverflow.com/questions/11783932/how-to-add-linker-or-compile-flag-in-cmake-file) – usr1234567 Jul 16 '16 at 18:10

2 Answers2

2

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

neerad29
  • 463
  • 1
  • 3
  • 15
-1

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.

Cristiano Araujo
  • 1,632
  • 2
  • 21
  • 32
  • -l is a linker flag, your non-solution just works by chance. Have a closer look at http://stackoverflow.com/questions/11783932/how-to-add-linker-or-compile-flag-in-cmake-file – usr1234567 Jul 16 '16 at 18:09