Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » C / C++ IDE (CDT) » How to set up library with project file for C++(Can't create objects from library or use functions from library)
How to set up library with project file for C++ [message #1765382] Fri, 09 June 2017 02:13 Go to next message
Kim Lytle is currently offline Kim LytleFriend
Messages: 2
Registered: June 2017
Junior Member
Hello,

I've been stuck on this problem for a while and have been madly searching every website and YouTube tutorial I can find for answers.

I have created a very basic (dynamic) library in Eclipse's C++ IDE called TestLibrary which includes a Greetings class (greetings.cpp and greetings.h) that should print to the console "hello" or "goodbye" based on the function that's called.

I cannot link these files to the project as I have seen in many tutorials that basically all say to go into the project file's properties and include paths under the compiler and linker sections.

The farthest I've gotten is getting no errors about including these file paths but not being able to use an object from the library appropriately. I can print normally to the console if there is no object created, but if I create an object, nothing will print, not even a generic std::cout call. I cannot get this far unless I use a dynamic library.

Any help would be greatly appreciated.
Re: How to set up library with project file for C++ [message #1765865 is a reply to message #1765382] Wed, 14 June 2017 04:28 Go to previous messageGo to next message
David VavraFriend
Messages: 1426
Registered: October 2012
Senior Member
There are a number of ways to do this. Some are outdated. This is what I do.
Three things need to be told about your library:

1) the Indexer, which needs to know where to look for header files.

I have a small utility library in $HOME/proj/LOCALLIB
I need to inform the indexer of where it is
Project --> Properties --> C/C++ General --> Preprocessor Include Paths, Macros etc. --> Entries tab
index.php/fa/29633/0/
The project also uses the openmpi library but it is found in the built-in include paths so it isn't necessary to additionally specify it.

2) If Eclipse is generating the makefile, the compiler needs to know the locations of the headers

Project --> Properties --> C/C++Build --> Settings --> Tool Settings Tab --> compiler
index.php/fa/29634/0/
If you are generating the makefiles you will need to add the -I<header path> manually to the compile command

3) If Eclipse is generating the makefile, the linker needs to know how to find the library
Generally you would use Project --> Properties --> C/C++Build --> Settings --> Tool Settings tab --> linker --> Libraries to set the library path (-L) and library files (-l).
Here I use /usr/lib64/openmpi/lib/libopenmpi.so and /usr/lib64/openmpi/lib/vt-mpi.a
You should familiarize yourself with what these options mean to the GCC loader.
ftp://ftp.gnu.org/old-gnu/Manuals/ld-2.9.1/html_node/ld_3.html
https://gcc.gnu.org/onlinedocs/gcc-5.2.0/gcc/Link-Options.html

index.php/fa/29635/0/

The library name for my local library doesn't follow the GNU conventions for library names so I can't use -l<name> (which is shorthand for lib<name>.<a or so> see the loader options links above). Instead I need to add it as a Miscellaneous --> Other Object

index.php/fa/29636/0/

Obviously, if you are generating the makefile you will need to add the options manually to the link command.

Here's the resulting compile and link.
Note the use of pkg-config to get the parameters for the opencv package.
This likely may not work in Windows.
These are set in
Project --> Properties --> C/C++Build --> Tool Settings Tab --> compiler --> Miscellaneous appended to Other flags
-c -fmessage-length=0 -pthread `pkg-config --cflags opencv`
and Project --> Properties --> C/C++Build --> Tool Settings Tab --> linker appended to the Expert settings field
${COMMAND} ${FLAGS} ${OUTPUT_FLAG} ${OUTPUT_PREFIX}${OUTPUT} ${INPUTS} `pkg-config --libs opencv`

Building file: ../src/Hello.cpp
Invoking: GCC C++ Compiler
g++ -std=c++0x -I/home/dvavra/proj/LOCALLIB/inc -O0 -g -Wall -c -fmessage-length=0  -pthread `pkg-config --cflags opencv` -MMD -MP -MF"src/Hello.d" -MT"src/Hello.o" -o "src/Hello.o" "../src/Hello.cpp"
Finished building: ../src/Hello.cpp
 
Building target: Hello
Invoking: GCC C++ Linker
g++ -L/usr/lib64/openmpi/lib -o "Hello"  ./src/Florp.o ./src/Hello.o ./src/ParmTest.o  /home/dvavra/proj/LOCALLIB/lib/locallib.a -lmpi -lvt-mpi `pkg-config  --libs opencv`
Finished building target: Hello


HTH

[Updated on: Wed, 14 June 2017 04:48]

Report message to a moderator

Re: How to set up library with project file for C++ [message #1766606 is a reply to message #1765865] Fri, 23 June 2017 12:22 Go to previous messageGo to next message
k k is currently offline k kFriend
Messages: 2
Registered: June 2017
Junior Member
I followed these instructions exactly, and it's still not working

doesn't this seem like the perfect repetitive bookkeeping that the program should handle on its own once you hand it the library directory and name?
Re: How to set up library with project file for C++ [message #1766930 is a reply to message #1766606] Wed, 28 June 2017 19:40 Go to previous messageGo to next message
David VavraFriend
Messages: 1426
Registered: October 2012
Senior Member
Repetitive in what way? You only need to inform Eclipse once about the location and name of the library and only if Eclipse is generating a makefile for you.
It's a subject with many variations. I gave examples of some of them.
There were no instructions.
Eclipse is really just an editor with the capability of calling external tools to perform a build and later to debug the build.
You need to understand how the tools that Eclipse is using want the information.


Re: How to set up library with project file for C++ [message #1767102 is a reply to message #1765865] Sat, 01 July 2017 00:53 Go to previous message
Kim Lytle is currently offline Kim LytleFriend
Messages: 2
Registered: June 2017
Junior Member
Thank you so much for your help, David, and I apologize for the late reply. Your explanation was very clear and helped me pinpoint the fact that I was missing the linking step. Additionally, I now understand the entire process much better. That project now runs perfectly.

I am now working with a git project that uses the program_options library; I'm not sure if you are familiar with this one. In this new project, I have added the program_options library in a similar fashion to how you described adding a normal library. I have found that I do not have control over linking libraries for projects imported from git (I'm not sure why this is. I am unable to see the Tools tab under Properties > C/C++ Build > Settings). I did as much of the other steps as I could, and I had no linking errors in one of two files in my project directory. The second project, however, said it did not know how to include "C:\boost\program_options.hpp" and would not produce any output after building and running the project. I narrowed down the problem to be in the actual program_options.hpp file (http://www.boost.org/doc/libs/1_55_0/boost/program_options.hpp), where that file itself says it can't locate any of the files it is trying to include (e.g. options_description.hpp, positional_options.hpp, etc). Do you know why this might be or how I could fix it? I've seen that program_options must be "built separately"; maybe that has something to do with it?


Thank you!

Previous Topic:Syntax error reported for designated structure initializers when returning an anonymous structure
Next Topic:crashes following upgrade to debian stretch
Goto Forum:
  


Current Time: Tue Apr 23 08:33:11 GMT 2024

Powered by FUDForum. Page generated in 0.02931 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top