0

I'm trying to link this external library which i have compiled using cmake which give me libfc.a file. I tried to link this static library to my main.cpp file but unable to access static library.

//main.cpp
int main()
{
    fc::unsigned_int  abi_sequence  = 0;
}

I've tried:

g++ main.cpp libfc.a -o main.out
g++ main.cpp -L. -lfc -o main.out

I've tried other examples from other stackoverflow posts but i didn't help me at all.

  • 1
    What are the errors you get when you build? And do you include the ***header file*** which defines the `fc` namespace (or class) and the `fc::unsigned_int` type? – Some programmer dude Oct 28 '19 at 13:23
  • Your issue is about order. The libraries to link with must be specified right after the module that depends on them. – Fareanor Oct 28 '19 at 13:26
  • 1
    @Fareanor Libraries should be after the modules (object or source file) that depends on them, they don't have to be absolute last. – Some programmer dude Oct 28 '19 at 13:29
  • @Someprogrammerdude You're right, my bad (corrected). – Fareanor Oct 28 '19 at 13:29
  • @Someprogrammerdude, i'm receiving `main.cpp: In function ‘int main()’: main.cpp:3:5: error: ‘fc’ has not been declared fc::unsigned_int abi_sequence = 0;`. Do i have to also include header which define this type? – Farhan-TroonTechnologies Oct 28 '19 at 13:30
  • @Fareanor, I've also tried that example but answer didn't work for me. – Farhan-TroonTechnologies Oct 28 '19 at 13:31
  • 1
    @Farhan-TroonTechnologies Yes you need to include the header files as well. The header files defines things like classes, namespaces, types, etc. The library file itself contains the *code*, the implementations of the functions themselves. – Some programmer dude Oct 28 '19 at 13:32
  • @Farhan-TroonTechnologies Of course you have to include the header. – Fareanor Oct 28 '19 at 13:32
  • @Fareanor, library headers are in include folder and main files are in src folder. When i include a header then it unable to find other header file. Can you send me link or any example that could help me? – Farhan-TroonTechnologies Oct 28 '19 at 13:36
  • @Farhan-TroonTechnologies if you don't use a buildsystem such as cmake, that has to be done manually with `-I./my_include_dir` – Guillaume Racicot Oct 28 '19 at 13:38
  • You have to write the path if the header is not in the same directory. For example, if the current source file is in _cpp_ folder, you can write `#include "../include/my_header.h"`. Or better, specify the include directory in the compilation command as @GuillaumeRacicot did suggest. – Fareanor Oct 28 '19 at 13:38
  • @Fareanor, yeah header are not in same directory. I have tried it but it contain a lot of header and i need to change on all of them. Is there any other solution where i don't have to change the library code? – Farhan-TroonTechnologies Oct 28 '19 at 13:40
  • @Farhan-TroonTechnologies Yes, read the comment of Guillaume Racicot. This is way better and what you need. – Fareanor Oct 28 '19 at 13:41
  • @GuillaumeRacicot, I just checked it. Thank you so much :) – Farhan-TroonTechnologies Oct 28 '19 at 13:43
  • @Farhan-TroonTechnologies I'd suggest you to checkout a build system such as CMake (modern cmake) that will automate the configuration of the whole project. – Guillaume Racicot Oct 28 '19 at 13:45
  • @GuillaumeRacicot, I will check that. Thanks to you, i'm able to specify the header path but it's couldn't find src path or definition which give me error. `undefined reference to fc::sha256::sha256()` – Farhan-TroonTechnologies Oct 28 '19 at 13:55

0 Answers0