[SOLVED] I can`t link my static library to my qt project
-
Hello, I have a custom utilities written in C, then outputed to static library. I`ve made include folder with the headers and lib folder with the so called "libutilz.a". In Qt creator pro file:
@
LIBS += -L/PATH_TO_LIB/lib/ -lutilz
@Then when I compile it gives me
@
:-1: error: collect2: error: ld returned 1 exit status
/home/ilian/QT5/DELETEME/main.cpp:-1: error: undefined reference to_getUtils()' @ [EDIT] It gives me undefined ref to my function which is in the .h file [EDIT] I
ve solved it. I read about name mangling and understood that, actually, I had to wrap in extern "C" the include of my header like that:
@
extern "C" {
#include "utils.h"
}
@
That fixed the problem. -
What operating system?
And how is libutilz.a built?
It sounds like it is an invalid library. That is the behavior you see when Qt/gcc can't find any symbols in the library you are linking.
-
If you are in Linux, what does the following command show:
$ ar t libutilz.a
-
libutilz was build with ar rcs libutilz.a myutil.o
ar t libutilz.a reported me myutil.o -
You can try linking directly to the lib:
@
LIBS += /my/path/libutilz.a
@It is not able to use the library in your build. I don't know why but that is what is going on. It looks like the library has the myutil.o that you need, but for some reason it isn't working with your project.
My guess (at least what I usually see on these forums and from developers) is that it is a lib that is built with incompatible settings to what is being used by the app.
Most cases it is people trying to include a DLL built using MSVC in a project using mingw, or vice-versa.
-
Also, you can try taking Qt out of it. Write a small app that use the myutil.o functions in a main.cpp.
Then just build raw with gcc:
@
$ gcc -o test myapp.cpp -L/my/path/lib -lutilz
@ -
Will try it right on
[quote author="ambershark" date="1420502577"]Also, you can try taking Qt out of it. Write a small app that use the myutil.o functions in a main.cpp.Then just build raw with gcc:
@
$ gcc -o test myapp.cpp -L/my/path/lib -lutilz
@
[/quote] -
I`ve tried it:
@
Test strip on Some foo bar stuff
result: [Somefoobarstuff]
@
Supposedly working the way you suggested. -
Oppps. Correction. I`ve tested in on main.c. Now in main.cpp it gives me the same error.
-
I
ve fixed it. Pfff. It
s name mangling problem I should take note of from now on. Here is the fix:
in some of the main.cpp files when inlcuding "utils.h" do it like
@
extern "C" {
#include "utils.h"
}
@
I`ll add as sloved and fix.