B
B
BindShot2016-03-20 00:14:52
linux
BindShot, 2016-03-20 00:14:52

Why is the library not included in clion?

I can't figure out why Clion doesn't include the sqlite library. Through the terminal, the code compiles normally.
The code itself:

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

int main(int argc, char* argv[])
{
    sqlite3 *db;
    char *zErrMsg = 0;
    int rc;

    rc = sqlite3_open("test.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);
}

CMakeLists.txt
cmake_minimum_required(VERSION 3.4)
project(projectDB)

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

set(SOURCE_FILES main.cpp )

include_directories("/usr/include")

add_executable(projectDB ${SOURCE_FILES})

Compilation output:
[ 25%] Linking CXX executable projectDB
CMakeFiles/projectDB.dir/main.cpp.o: In function `main':
/home/peter/ClionProjects/projectDB/main.cpp:10: undefined reference to `sqlite3_open'
/home/peter/ClionProjects/projectDB/main.cpp:13: undefined reference to `sqlite3_errmsg'
/home/peter/ClionProjects/projectDB/main.cpp:17: undefined reference to `sqlite3_close'
collect2: error: ld returned 1 exit status
make[3]: *** [projectDB] Ошибка 1
make[2]: *** [CMakeFiles/projectDB.dir/all] Ошибка 2
make[1]: *** [CMakeFiles/projectDB.dir/rule] Ошибка 2
make: *** [projectDB] Ошибка 2

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andryukha, 2016-03-20
@syrov

It is necessary to link the library, the header is not enough. Look at target_link_libraries, find_package, in general, don't be lazy to launch a search engine. And clion is out of business, he doesn't care.

A
apu3, 2020-08-13
@apu3

See the code below, but for now the theoretical background of all this:
If you want to understand what make and CMake are, then you should read:
First about make (make is a utility that eliminates the need to write g++ main.cpp -o main every time -I ... -l ... etc):
1. Just about make - https://habr.com/ru/post/211751/
2. Makefile for the smallest - https://habr.com/ru/ post/155201/
Then about CMake (you can write make files, but this is not very convenient and is fraught with errors due to inattention. Therefore, we created CMake, which is used in CLion):
1. Introduction to CMake - https://habr.com /en/post/155467/
The code itself, which should solve the problem:

cmake_minimum_required(VERSION 3.17)

# TODO: Здесь указано название проекта. Поменяйте на свой!
project(sqliteLearning)

# Задаём стандарт C++ (в данном случае - C++ 14 года)
set(CMAKE_CXX_STANDARD 14)

# TODO: Здесь первое название - будущее название исполняемого файла (в моём 
# случае - это sqliteLearning, вы можете выбрать любое), а второе название (main.cpp) - 
# это файлы с исходным кодом, их может быть несколько)
add_executable(sqliteLearning main.cpp)

# Пытаемся найти SQLite3
find_package (SQLite3)
# Если нашли, то:
if (SQLITE3_FOUND)
    # Сообщаем об успехе в консоль (это необязательно)
    message("Found sqlite3, and...")
    # (Указываем место, где будем искать заголовочный файл. ${SQLite3_INCLUDE_DIR} - 
    # это переменная, которая автоматически создаётся, если сработала команда find_package(SQLite3))
    include_directories(${SQLite3_INCLUDE_DIR})
    # Прилинковываем библиотеку SQLite3
    target_link_libraries (sqliteLearning ${SQLite3_LIBRARY})
endif (SQLITE3_FOUND)

# Следующие две опции могут помочь, если остальное не помогает, но помните, что это костыль
#add_compile_options(-l sqlite3)
# Нет нужды применять обе строчки одновременно
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lsqlite3")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question