How to add a library to eclipse C project? - c

I need to add a C library to C project in Eclipse indigo.
What I've done so far.
I've downloaded and installed the library.
All headers are in folder /usr/include, library.so files are in folder /usr/lib.
After having read a tutorial I tried to do it like this:
When in Eclipse -> Project -> C/C++ Build -> Settings -> GCC C Linker -> Libaries -> Add libary -> "ibxml2" ( library name is libxml2 )
But it still doesn't work.
I am a super newbie at Linux, so please could you give me some advise how to do that?
By the way one more thing. After creating a new C/C++ Project when I run an error occurs " Launch failed! Binary not found". I checked "Binary Parsers" in Project's Settings and "Elf Parser" is set ( I've read that it is OK for Linux ), so what is wrong? It is not so important, since it works when I first only build project and then run it, but maybe there is a way to solve that too?

In linker options, you should not add "libxml2", but just "xml2". All binary libraries in Linux (so files) have a "lib" prefix that is not a part of library name.

Related

Having Difficulty Integrating JSON Library with My AVR Microcontroller Code

I am a relatively inexperienced C developer with no previous experience in integrating libraries made by other developers into existing projects.
Basically, I need a means of parsing JSON data in an AVR microcontroller for a university project. To this end I attempted to download and integrate jansson (https://github.com/akheron/jansson) into my existing build of the microcontroller code. I am working with Atmel Studio in Windows 10, but I have also installed Code::Blocks with MinGW GCC (on the same Windows 10 installation) for the purpose of building the library, and to attempt to integrate the library into a native Windows application. So far, neither has been successful, and I get the same errors. All of the online resources I've found so far have been to basic to be useful, or well beyond my comprehension.
This is what I have done thus far:
I began by attempting to build the software and then integrate it into an existing project per the instructions in https://jansson.readthedocs.io/en/2.11/gettingstarted.html. I installed CMake, built the project files for Code::Blocks with cmake.exe -G “CodeBlocks - MinGW Makefiles”, then opened the project and built everything. A few of the targets (I believe related to testing) failed to build, but jansson itself built and output libjansson.a to the \lib\ directory, so I didn’t think too much of it.
That is as far as I’ve been able to get. In both Atmel Studio and Code::Blocks, I do the same thing: add jansson.h to the relevant include paths, add #include “jansson.h” to all of the relevant files, and add libjansson.a as a library in each IDE’s respective linker options. I’ve tried various things like adding and removing flags to the linker, but the output is always “cannot find -ljansson”, “undefined reference to ‘json_object_seed’” (which is a function in the API I’m calling for no reason other than to see if the project has built properly) and/or “ld returned 1 exit status”.
I cannot help but feel as if the issue is with the line “cc -o prog prog.c -ljansson” in the documentation linked above. I really just don’t understand how to set up the linker properly to get the project to build.
If anyone could give some insight into what I’m doing wrong/the correct way to link this library I would appreciate it a lot.
The library itself should be built with appropriate toolchain. I assume that you built your library twice, one version using MinGW toolchain and other with avr-gcc toolchain.
If you compile target application and linker cannot find library, then try to add path of directory that contains *.a file of library to linker settings (linker search path). Let's say you have: /path/to/lib/libjansson.a
In Code::Blocks: Project → Build options → Search directories → Linker add /path/to/lib/. Then it should link with include path set, for example: cc -o prog prog.c -ljansson -L/path/to/lib/
In Atmel Studio when you add a library in Solution Explorer → Libraries → Add Library it should automatically add library search path to linker options. If you check Project → Properties → AVR/GNU Linker there should be (between other options): -Wl,-ljansson -Wl,-L"/path/to/lib/"
If you copied library files (libjansson.a and jansson.h) to your application's project directory, it will be convenient to use relative paths to library files.

Eclipse CDT - C project

I'm new using Eclipse for programming in C.
I have several doubts and although I found a lot of questions related to it, by now I don't get to solve my problems.
I have a C project with several source files(.c), header files(.h), a Makefile and an archive library (.a). Until now, I have been editing my code with a editor (gedit) and I have been compiling it with the terminal. I run "make" on the terminal and several shared object libraries (.so) are generated and in addittion, an executable file.
Now, I want to use Eclipse with CDT to programme and compile the code. My first doubt is the type of project that I must choose:
Executable or Makefile project?
I selected the "Executable" type. Then I tried to build the project and I got a lot of errors due to "undefined references" and several error messages with "relocation x has invalid symbol index".
I did the following steps:
Properties -> C/C++ General -> Path and Symbols -> Includes -> GNU C -> Add -> Filesystem ->
I put the project folder, since the header files are in this folder (with absolute path). Then "Clean project" and "Build project". It didn't work, and I got the same errors
Later, I tried:
Properties -> C/C++ Build -> GCC C Linker -> Miscellaneous -> Other objects ->
I put the archive file (.a). Then "Clean project" and "Build project".
The previous problems didn't occur, but I got other "undefined references" with files related to the archive file and I can't access them.
I appreciate any help.
I'm sorry if someone already asked this question, but I searched a lot before I write it.
Thanks
I run "make" on the terminal and several shared object libraries (.so) are generated and in addittion, an executable file.
Each .so and each executable should be a separate project in your workspace.

How to Install and run iniparser for Eclipse CDT?

I have been trying to find a way to install the iniparser library to write configuration files for C programs in eclipse, the whole day today. But I have not been able to find anything usefull. I am using Windows 7 (64 bit).
I have tried downloading the zip file and loading it into the 'lib' folders of eclipse. But when I try to include it into the C program it gives me a error saying 'iniparser.h' not found.
Can somebody PLEASE tell me how can I do this?? I would really be thankfull..
You can include path-
Try-
Project -> properties -> C/C++General -> Paths and Symbols -> Includes tab -> Assembly
while checking 'add to all languages'.
This one did not work for me- Project -> Properties -> C/C++General -> Paths and Symbols -> Includes tab -> GCC C

Cannot find -lpthread?

I am new to C programming.
I was trying to use the pthread API to write some concurrent program.
I downloaded eclipse IDE for C/C++ Developers, MinGW. I have put all the library, header files into the corresponding location of the MinGW file.
When I tried to build the project, there is always an error "cannot find -lpthread", what happened?
I have added the "-pthread" to the GCC compiler.
I have searched a lot in Google but seems no one have similar problem as me.
The answer to this question by someone who is also missing MinGW pthread library should help you out! Essentially the issue is that the MinGW installer script might not download the lpthread library upon installation. Quoted from link:
Just run and open MinGW Installation Manager, which should be pre-installed with MinGW, select "All Packages" on the left panel, and on the right panel, search for "mingw32-pthreads-w32" packages and install them.
I downloaded eclipse IDE for C/C++ Developers, MinGW.
MingGW uses the Windows API. The Windows API does not provide PThreads.
You need to install PThreads for Win32 to have PThreads available under Windows, and with this available under MinGW.
Eclipse is not configured to put the -pthread argument in the gcc compilation. To solve this, go to the Menu:
view sourceprint?
1.Project -> Properties
From the bar on the left:
view sourceprint?
1.c/c++ build -> GCC C Compiler -> Miscellaneous
Add the “-pthread” argument into the beginning of the “Other Flags” Also go to:
view sourceprint?
1.c/c++ build -> Settings -> GCC C Linker -> Libraries
And include the “pthread”library into the other libraries. Click Apply and rebuild the project. Pthreads must work now.
See the question on mingw.org. I ended up with adding 'C:/cygwin/lib' to the settings for the "Library search path (-L)" at properties >> c/c++ build >> settings >> MinGW C Linker >> Libraries.

How to link a ".so" library in Microsoft Visual Studio

I was wondering if anyone knew how to link a .so dynamic library (or a .a static library for that mater) in Microsoft visual studio. The following steps have not worked for either .a or .so.
I'm playing around with the vs-android plugging to develop in Android NDK using MVS and I'm trying to add a custom library, compiled as a .so, to the project.
1 - I've created a dynamic library (.so) project alongside one of the sample projects (the san-angeles project), in the same solution.
2 - I've set the .so project, let's call it "engine" as a dependency of san-angeles. The engine.so file is generated without any problems, and appears in the {SolutionFolder?}/Android/Debug folder.
3 - I've added engine.so to the linker additional includes of san-angeles and the {SolutionFolder?}/Android/Debug folder (as a full path, something like C:/projects/.../Debug) to the additional linker directories.
But when I compile san-angeles I get the error:
"arm-linux-androideabi-g++.exe: engine.so: No such file or directory"
So I'm guessing I need to do something else to add a .so/.a to a project? Maybe change the name to libengine.so or something like that? A lot of the default includes have the lib prefix, so I don't know if it's something along those lines.
Thank you for the help,
Jaime
For anybody stumbling on this problem:
Add the directory where the .so resides in Linker -> General -> Additional Library Directories. The project you are compiling must be also a Dynamic Library .so for the linker to be available. This appends the -L flag to the commandline
In the Linker -> Command Line append your .so to the Additional Options with the -l flag with quotes, e.g. -l"MyDynamicLibraryWithoutLibPrefixAndExtension". In my case I wanted to link libassimp.so -> -l"assimp"
Ok, so in my case this was solved by:
1 - Naming the generated engine.a as libengine.a
2 - Instead of, from MVS, adding engine.a to Preferences/Linker/Additional Dependencies, I added to Preferences/Linker/Command Line -l"engine"
With this, the project finally managed to find and engine.a
Hope this helps someone else. :)

Resources