Notes

CMake notes

author: Paul Kim

categories: cmake, c

tags: cmake, c

CMake notes

Don't forget to add target_link_libraries line to CMakeLists.txt after add_executable when you include libraries in your C code (i.e., #include <math.h> or #include <sqlite3.h>). Otherwise, you will get compiler errors.

math.h

#include <stdio.h>
#include <math.h> // don't forget to link this library in CMakeLists.txt

int main() {
  float val1;

  val1 = 1.6;

  printf("value1 = %.1lf\n", ceil(val1));

  return (0);
}

cmake_minimum_required(VERSION 3.14)
project(my_project C)

set(CMAKE_C_STANDARD 99)

add_executable(my_project main.c)
target_link_libraries(my-project m) # don't forget to add this line

sqlite3.h

#include <stdio.h>
#include <sqlite3.h> // don't forget to link this library in CMakeLists.txt

int main(int argc, char *argv[]) {
  sqlite3 *db;

  if (sqlite3_open("test.db", &db)) {
    fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
    return (0);
  } else {
    fprintf(stderr, "Opened database successfully\n");
  }
  sqlite3_close(db);
}

cmake_minimum_required(VERSION 3.14)
project(my_project C)

set(CMAKE_C_STANDARD 99)

add_executable(my_project main.c)
target_link_libraries(my-project sqlite3) # don't forget to add this line

Source

ChromeEdgeFirefoxOpera

© 2021 paulkode.com. All rights reserved.