0

I created a project library in Clion and got a *.a file. In qt_gui.pro I added:

LIBS += -L /path/to/*.a/file
INCLUDEPATH += /path/to/directory/h_files

Also I created new .h file with:

extern "C" {
    #include "foo.h"
}
void qt_foo();

And .cpp file:

void qt_foo(){
    foo();
}

When I calling qt_foo() I getting

error: undefined reference to `foo'

foo.h in library:

#ifdef __cplusplus
extern "C" {
#endif
void foo();
#ifdef __cplusplus
}
#endif
Daniel Mur
  • 15
  • 4
  • You must also add the header, to use the code from c to c ++ check the following link: https://stackoverflow.com/questions/16850992/call-a-c-function-from-c-code – eyllanesc Nov 18 '17 at 15:22
  • @eyllanesc Do I need to add .h files to the qt project? – Daniel Mur Nov 18 '17 at 15:25
  • Use `INCLUDEPATH += /path/of/header-directory` – eyllanesc Nov 18 '17 at 15:29
  • Try with `extern "C" { foo(); }` – eyllanesc Nov 18 '17 at 15:56
  • @eyllanesc nothing changed :( – Daniel Mur Nov 18 '17 at 16:11
  • The problem is related to your [`LIBS` statement](http://doc.qt.io/qt-5/third-party-libraries.html#library-files): `LIBS += -L/path/to/library/folder -lmylibrary`, or alternatively (not portable) `LIBS += /path/to/my/library/libmylibrary.a` – m7913d Nov 18 '17 at 16:41
  • Related post: https://stackoverflow.com/questions/718447/adding-external-library-into-qt-creator-project – m7913d Nov 18 '17 at 16:45
  • Note that _"undefined reference"_ is a linking error and has nothing to do with header include directories. – m7913d Nov 18 '17 at 16:46
  • @m7913d yep, thanks you, but works only alternatively way (LIBS += /path/to/my/library/libmylibrary.a). What could be the problem? – Daniel Mur Nov 18 '17 at 17:01
  • Are you sure you added `-lmylibrary`? I would expect that qt displays an error when he is not able to locate a library. Have you checked the compile output window? – m7913d Nov 18 '17 at 17:24
  • @m7913d yes, qt displays "not found" – Daniel Mur Nov 18 '17 at 17:53
  • @DanielMur What is your exact library name and `LIBS` command? If the name of the library is `libMyLibrary.a`, you should write `LIBS += -lMyLibrary`, i.e. remove the `lib` prefix. – m7913d Nov 18 '17 at 18:13
  • @m7913d I understand. thanks you – Daniel Mur Nov 18 '17 at 18:23

0 Answers0