C/C++ undefined reference for sqlite3's functions using CLion with CMAKE

5k Views Asked by At

CMAKE's file has this code:

cmake_minimum_required(VERSION 3.6) 
project(HelloSqliteC)

set(CMAKE_CXX_STANDARD 11)

set(SOURCE_FILES main.c)
add_executable(HelloSqliteC ${SOURCE_FILES})

The file main.c has this code:

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

int main() {
   sqlite3 *db;
   int rc;

   rc = sqlite3_open("database.db", &db);

   if (rc) {
       fprintf(stderr, "Can't open database: %s!\n",      sqlite3_errmsg(db));
   } else {
       fprintf(stderr, "Opened database successfully!\n");
   }

   sqlite3_close(db);
   return 0;
}

When I've trying compiling:

/home/marcus/ide/clion/clion-2016.3.1/bin/cmake/bin/cmake --build      /home/marcus/projects/native/HelloSqliteC/cmake-build-debug --target   HelloSqliteC -- -j 4
[ 50%] Building C object CMakeFiles/HelloSqliteC.dir/main.c.o
[100%] Linking C executable HelloSqliteC
CMakeFiles/HelloSqliteC.dir/main.c.o: In function `main':
/home/marcus/projects/native/HelloSqliteC/main.c:13: undefined reference to `sqlite3_open'
/home/marcus/projects/native/HelloSqliteC/main.c:16: undefined reference to `sqlite3_errmsg'
/home/marcus/projects/native/HelloSqliteC/main.c:21: undefined reference to `sqlite3_close'
collect2: error: ld returned 1 exit status
CMakeFiles/HelloSqliteC.dir/build.make:94: recipe for target     'HelloSqliteC' failed
make[3]: *** [HelloSqliteC] Error 1
CMakeFiles/Makefile2:67: recipe for target     'CMakeFiles/HelloSqliteC.dir/all' failed
make[2]: *** [CMakeFiles/HelloSqliteC.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target     'CMakeFiles/HelloSqliteC.dir/rule' failed
make[1]: *** [CMakeFiles/HelloSqliteC.dir/rule] Error 2
Makefile:118: recipe for target 'HelloSqliteC' failed
make: *** [HelloSqliteC] Error 2

I tried solve this issue using different ways, but no success. I'm using CLion C/C++, my OS is Ubuntu 16.04 and I install sqlite3 using autoconf.

For test, I used the main.c above and compiled in command line with "-l sqlite3" using GCC and I had success, but I want use CLion.

Help me, thanks.

3

There are 3 best solutions below

0
On BEST ANSWER

I solved this issue with this target_link_libraries(HelloSqlite3 LINK_PUBLIC sqlite3) or target_link_libraries(projectName LINK_PUBLIC libraryName).

2
On

CLion uses CMake for all the building and project configuration. You have to manually modify CMakeLists.txt. In fact this is a CMake question.

This line in your CMakeLists.txt will solve your problem:

add_compile_options(-l sqlite3)

But actually CMake has a more sophisticated dependency discovery system. Read How To Find Libraries to learn this.

1
On

I also use CLion C/C++. The OS is Ubuntu 18.04. But I want to compile the main.cpp. The CMakelist.txt is:

cmake_minimum_required(VERSION 3.13)
project(ProjectName)

set(CMAKE_CXX_STANDARD 14)
add_executable(student main.cpp)
target_link_libraries(ProjectName LINK_PUBLIC sqlite3)

It works successfully.