1

I am fairly new to C++ and i am trying to use a precompiled C library (with .a file format) in my code.

I have a simple CMake project setup and a .a library written in C. How do i correctly include it and add it to my CMake file?

JustClaire
  • 451
  • 3
  • 11
  • What compiler are you using? GCC? – Fiddling Bits Jul 02 '20 at 18:22
  • Do you have an accompanying header file? – Eugene Sh. Jul 02 '20 at 18:23
  • @Fiddling Bits Yes, GCC – JustClaire Jul 02 '20 at 18:24
  • 2
    Have you seen the responses [here](https://stackoverflow.com/q/8774593/3987854), particularly you might try linking to the .a library using the answer [here](https://stackoverflow.com/a/10550334/3987854). Also, please show the code you have tried so far, and what, if any, errors or unexpected behavior you are receiving. – Kevin Jul 02 '20 at 18:25
  • @Eugene Sh. Yes, i do – JustClaire Jul 02 '20 at 18:25
  • 1
    You will need a header which declares all the functions from the library. It will either contain `#ifdef __cplusplus` / `extern "C" {` / `#endif` / …main contents… / `#ifdef __cplusplus` / `}` / `#endif` or equivalent, or you will include it where needed `extern "C" {` / `#include "header.h"` / `}` so that the functions are known to the C++ compiler to be C functions and not C++ functions. Add the library (and its directory if necessary) to the linking command line (`-L/where/it/is/lib`, `-lwhatever`). Add the header directory to the compiling command lines (`-I/where/it/is/include`). – Jonathan Leffler Jul 02 '20 at 18:30
  • @squareskittles thank you, the question you linked helped me! i wasnt linking it through cmake correctly – JustClaire Jul 02 '20 at 18:30

0 Answers0