Link object file to my project Eclipse CDR - c

I'm working on a project from school, and we were given a .o and a corresponding .h file.
We need to use several functions from the .o file in our .c program.
Just placing it in the same directory doesn't work.
I tried to look for something like this in the project properties, but no good.
I keep getting ../code_files/Search.c:116: undefined reference to 'reportError'
I'm using Eclipse (Juno) CDT, gcc MinGW under Windows 7
I know it's possible to include .a files, but I couldn't find any indication on how to include a .o file
#include "ErrorHandle.h" is included in the main c file.
Anyone knows how to include a .o file to a project?
Thanks!

I found this answer:
I tried doing something similar, only I didn't think of the miscellaneous thing

You can trivially turn the .o into a .a with ar cvs library.a object.o, and then add the .a to your project.

Related

Adding custom package to buildroot

Someone can help me to write an .mk file and add custom library to buildroot.
Example:
I have foo.c and foo.h and this library that i wrote, have to be installed on a custom Linux to make my main app running successfully.
I also want to know if until this Linux was deployed, Could I use <foo.h> instead of ".../foo.h"
thanks in advance.
As I understand it you have a simple library with a .c and .h.
The Makefile needs to do the following.
Compile the .c to a .o
Put the .o inside a .a (Static library).
Put the .a (static library) into the "build root" as you call it. If the "prefix/build root" chosen is /usr/local (the default in most cases) the correct place is /usr/local/lib.
Copy the .h to the "build root". The correct place is "include". "/usr/local/include".
You can see a simple Makefile example here. Also you can make the project a single header project. Like here

how to recompile include files in eclipse

i want to recompile my include files of my project,which includes some header files and .c source files which are files for my Ethetnet driver. Now i want some change in one of my included source file. but when i change and build or rebuild my project, the change in the include .c source file does occur in the final output binary. that means my project taking previously compiled included .o files. So how can i recompile my all include files of the project so that change occur in final output binary.
Thanks in advance.
CMIIW but AFAIK it depends on your compiler (which I guess is gcc), dependency analysis against included files may be done or not, and in gcc case it does NOT do it. It only compares .c against its corresponding .o, so you have to force rebuilding when you change the include file. There perhaps certain compiler options you can use, but I don't know for it.
EDIT:
Just found a similar question: How can I have a Makefile automatically rebuild source files that include a modified header file? (In C/C++)
If you are indeed including the right file (the ones that you have modified, not some with same name from some other directory) then cleaning a project and rebuilding it should help. Just select a project in project explorer, right click, do "clean project", then build it...

Must C libraries have .lib extension

I don't know C, but need to interact with some C files in a project. I'm noticing that some files have .lib extension, while others (which are also supposed to be libraries) have .c and .h files only in a big folder.
What's the difference between these libraries.
Are the .c and .h folders also libraries.
Is the .lib format the official format for libraries and these guys who did .c and .h just lazy or not using best practice?
.c and .h files are source code, i.e., text files. In order to "use" them (i.e., execute the code on a computer) you need to compile them into...
a .lib file is the end result, i.e., a binary file. It can be statically lined into another executable and the code run on a computer. This saves you the time of compiling the source if you don't need to.
.lib is just one common extension, but it doesn't really matter what the extension is as long as the file is valid. Point your compiler/linker to the .whatever library file and let 'er rip, it will all work out in the end.
C static libraries are usually compiled in .lib on Windows and .a or .so on Linux/Unix. But extension is just a matter of convenience: "do you have that lib in a repo!?"
as to .h and .c they are as valid, but just not compiled.
You can use both approaches without fear, even if the extension is .darthvader
Compiler does not care about the extension as long as the file is specified. I name my libraries .a. Commonly, source files are named as .c, and header files as .h. But this just for mere convenience, a compiler will work on any valid source file, no matter the name.
The standard file extension for programs written in C is .c, header files that come with a project carry the extension .h.
.lib is just some programmers choice to name their library file. It usually stands for a compiled binary which can be statically linked into another executable file. Other common file extension are .a and .so (especially on *NIX machines).

What is a file with extension .a?

I downloaded this: https://github.com/mongodb/mongo-c-driver
And now I'm trying to use it inside my C program, but I don't know what to do with the generated .a files. What are they? I couldn't find any information, not even in the GCC manual.
And I built it like so:
scons --c99
Also, can I use C99 libraries with my C89 program?
.a files are static libraries typically generated by the archive tool. You usually include the header files associated with that static library and then link to the library when you are compiling.
.a files are created with the ar utility, and they are libraries. To use it with gcc, collect all .a files in a lib/ folder and then link with -L lib/ and -l<name of specific library>.
Collection of all .a files into lib/ is optional. Doing so makes for better looking directories with nice separation of code and libraries, IMHO.

How do you actually use a C library?

I'm sure this question has been asked many times, but I can't figure this out. Bear with me.
So when you download a library, you get a bunch of .c and .h files, plus a lot of other stuff. Now say you want to write a program using this library.
I copy all the .h files into my project directory. It just doesn't compile.
Great, so then I get the library as a bunch of .dll's, and i copy the dlls into my project directory. Still doesn't compile.
How does this work?
What do you do, like right after creating the folder for your project? What parts of the library package do you copy/paste into the folder? How do you make it so that it can compile? Go through the steps with me please.
Where to put the .h files?
Where to put the .dll files?
How to compile?
Thanks.
(the library I'm trying to get working is libpng, I'm in windows with MinGW, and i'm looking to compile from command-line like usual.)
(from what i gather, you put the .h files in directory A and the .dll files in directory B and you can use -l and -L compiler options to tell the compiler where to find them, is this correct?)
Here's a brief guide to what happens when you compile and build a basic C project:
The first stage compiles all your source files - this takes the source files you've written and translates them into what are called object files. At this stage the compiler needs to know the declaration of all functions you use in your code, even in external libraries, so you need to use #include to include the header files of whatever libraries you use. This also means that you need to tell the compiler the location of those header files. With GCC you can use the -I command line to feed in directories to be searched for header files.
The next stage is to link all the object files together into one executable. At this stage the linker needs to resolve the calls to external libraries. This means you need the library in object form. Most libraries will give you instructions on how to generate this or might supply it ready built. Under Linux the library file is often a .a or .so file, though it might just be a .o. Again you can feed the location of the library's object file to GCC with the -L option.
Thus your command line would look like this:
gcc myProg.c -I/path/to/libpng/include -L/path/to/libpng/lib -lpng -o myProg.exe
(Note that when using the -l command line GCC automatically adds lib to the start of the library, so -lpng causes libpng.a to be linked in.)
Hope that helps.
Doing it under windows (supposing you user Visual Studio)
After unpacking add the library include directories to your projects' settings (Project -> Properties -> C/C++ -> Additional Include Directories)
Do the same thing for the Libraries Directory (Project -> Properties -> Linker -> Additional Library Directories)
Specify the name of the library in your Linker Input: Project -> Properties -> Linker -> Input -> Additional Dependencies
After this hopefully should compile.
I don't recommend adding the directories above to the Global settings in Visual Studio (Tools -> Options -> Project and Solutions) since it will create and environment where something compiles on your computer and does NOT compile on another one.
Now, the hard way, doing it for a Makefile based build system:
Unpack your stuff
Specify the include directory under the -I g++ flag
Specify the Library directory under the -L g++ flag
Specify the libraries to use like: -llibrary name (for example: -lxml2 for libxml2.so)
Specify the static libraries like: library name.a
at the end you should have a command which is ugly and looks like:
g++ -I/work/my_library/include -L/work/my_library/lib -lmylib my_static.a -o appname_exe MYFILE.CPP
(the line above is not really tested just a general idea)
I recommend go, grab a template makefile from somewhere and add in all your stuff.
You must link against a .lib or something equivalent i.e. add the ".lib" to the libraries read by the linker. At least that's how it works under Linux... haven't done Windows so a long while.
The ".lib" contains symbols to data/functions inside the .dll shared library.
It depends on the library. For examples, some libraries contain precompiled binaries (e.g. dlls) and others you need to compile them yourself. You'd better see the library's documentation.
Basically, to compile you should:
(1) have the library's include (.h) file location in the compiler's include path,
(2) have the library stubs (.lib) location in the linker's library path, and have the linker reference the relevant library file.
In order to run the program you need to have the shared libraries (dlls) where the loader can see them, for example in your system32 directory.
There are two kinds of libraries: static and dynamic (or shared.)
Static libraries come in an object format and you link them directly into your application.
Shared or dynamic libraries reside in a seperate file (.dll or .so) which must be present at the time your application is run. They also come with object files you must link against your application, but in this case they contain nothing more than stubs that find and call the runtime binary (the .dll or the .so).
In either case, you must have some header files containing the signatures (declarations) of the library functions, else your code won't compile.
Some 'libraries' are header-only and you need do nothing more than include them. Some consist of header and source files. In that case you should compile and link the sources against your application just as you would do with a source file you wrote.
When you compile, assuming you have the libs and the headers in the same folder as the sources you are compiling, you need to add to your compile line -L . -I . -lpng. -L tells the linker where to look for the library, -I tells the compiler where to look for the headers and -lpng tells the linker to link with the png library.
[Edit]
Normal projects would have some sort of hierarchy where the headers are in an /include folder and the 3rd party libs are in a /libs folder. In this case, you'd put -I ./include and -L ./libs instead of -I . and -L.
[Edit2] Most projects make use of makefile in order to compile from the command line. You can only compile manually for a small number of files, it gets quite hectic after that
Also,
you may want to look over Dynamic Loading support in various languages and on various
platforms.
This support is very handy in cases when you want to use a library optionally and you don't want your program to fail in case that library is not available.

Resources