0

I'm using linux and I have downloaded a tarball of the soem1.3.0 library that I want to use. I have compiled the library and now I would like to use it. Problem is, I don't know how to do this.

I have a folder called "Project" and in this folder I have a subfolder containing the compiled library (this subfolder contains more folders containing .a files and header and source files) called "Project/SOEM1.3.0". The project folder also has my main function source file, "Project/main.cpp". When I try to compile this main.cpp, the compiler (g++) says that the header files I'm trying to include are unknown.

I guess I have to tell the compiler where to find the library, but how do I do this? I tried copying the entire SOEM1.3.0 folder to /usr/lib, assuming my compiler would be able to find it but this didn't work. Can anybody tell me what to do?

Update: I have been able to include the headers by using "-I path/to/headers". But now the compiler/linker is complaining it cannot find the functions described in the header files. I tried using "-L path/to/libsoem.a -l soem -I path/to/headers" but without result. Any suggestions? Just to be clear, the libraries are called libsoem.a, libosal.a and liboshw.a, and the command i'm trying:

gcc -L SOEM1.3.0/lib/linux -l soem -l osal -l oshw -I SOEM1.3.0/soem -I SOEM1.3.0/osal
-I SOEM1.3.0/oshw/linux -o test main.cpp

1
  • 1
    There should be a file called configure which generates the correct make environment. Then run make. Commented Mar 19, 2014 at 19:04

1 Answer 1

2

To tell gcc where to look for the headers during compilation use the -I option (lib has nothing to do with the headers, and no libraries are needed for compilation, i.e. when you use the -c option).

You'll need to tell gcc for the link stage, e.g. when you create an executable with -o name, which libraries to use and where the libraries are. For demanding a certain library use the -l option: for libmath you would specify -lmath; accordingly with other lib* libraries. If those libraries are in a non-standard directory you use the -L option to tell that to gcc.; or you just put them in one of the system's normal lib directories, either manually or via make install.

Edit: I forgot to mention the unintuitive but logical requirement that libraries given to the linker with -l must appear after the library, object or source files which depend on them, i.e. usually in the end. The linker just cherry-picks the definitions needed at that point from a library, it (luckily) doesn't lump everything together.

Order between libraries is also significant, if any of them have unresolved dependencies.

Sign up to request clarification or add additional context in comments.

8 Comments

Great, that worked! I wonder what this "normal lib directory" you talk about is. Would I have to manually move all the header files to "usr/include" and all the .a files to "usr/lib"? And what about the course files?
The standard directories are /usr/lib for things that come with the system and /usr/local/lib for user installed stuff (but that distinction is just arbitrary). linkers look there automatically. The standard header directories are /usr/include and, consequently, /usr/local/include. These dirs have just been that way for 30 or 40 years. As to move files around: If you use them only for this project and you link statically (.a libs), no need to. If they are to become part of your "system" or you link dynamically, probably.
That copying is usually done by "make install" as root after, as suspectus correctly remarked, running a "configure" script with appropriate options (which can be shown by "configure --help". But we are straying into sysadmin land :-).
Now I am having the problem that the compiler recognizes the header files, but it doesn't recognize the functions that are described in these header files. I get undefined references to these functions. Isn't using "gcc -L path/to/.a-files" when trying to compile enough? Should I also specify all the source files in this gcc command?
I mentioned two options for linking, -l and -L ;-). Btw, the gcc manual is online available.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.