3

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.

Marcus
  • 773
  • 8
  • 11
  • 1
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – too honest for this site Dec 21 '16 at 17:38
  • I assume CLion has a dialog to add external libraries to you project. You'll need to do that for sqlite3. CLion will then properly rebuild the `CMakeLists.txt` for you. – nega Dec 21 '16 at 17:40
  • @Olaf, no this is specifically a CLion usage issue – nega Dec 21 '16 at 17:41
  • @nega: Did you read the dup? It is generic and also covers this issue. **How** to add li9braries is specific to the build-tool, but users are expected to figure out such things on their own. In other words: to RTFineM. – too honest for this site Dec 21 '16 at 17:45
  • @olaf which is exactly what makes this not a dupe. OP has demonstrated he knows how to fix the link failure in the generic (command line) sense. but is asking specifically about how to fix it in CMake and or CLion. – nega Dec 21 '16 at 17:51
  • @olaf my issue is about how to fix it it CLion in my question _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._ – Marcus Dec 21 '16 at 17:55
  • @mark42 i just gave CLion a quick spin. I didnt realize it was that simple of an IDE. see my answer https://stackoverflow.com/questions/41251474/how-to-import-zeromq-libraries-in-cmake/41252437#41252437 but substitue sql3 for zmq. – nega Dec 21 '16 at 17:58
  • @MarcusAdriano: We are not the CLion support forum. What did you try to fix it, why did it not work? What about the documentation did you not understand? – too honest for this site Dec 21 '16 at 18:00
  • @nega if I understand, your answer I have put source code of the sqlite3 in my src folder? – Marcus Dec 22 '16 at 02:40
  • @mark42 no i don't suggest that at all. read the question and answer again. compare the OP's `CMakeLists.txt` to the one i suggest in my answer. – nega Dec 22 '16 at 17:30
  • @nega Hey, thanks, your post solve my problem too, but this solve target_link_libraries(projectName LINK_PUBLIC libraryName) and this is very simple. – Marcus Dec 24 '16 at 04:03
  • If this is not a duplicate but specific to CLion, why are the tags [cmake] and [sqlite] used? – usr1234567 Dec 24 '16 at 11:22

3 Answers3

4

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.

wang.xuhao
  • 61
  • 2
  • 5
2

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

Marcus
  • 773
  • 8
  • 11
1

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.

Eduard Wirch
  • 9,785
  • 9
  • 61
  • 73