Author Topic: adding library files and getting function declaration  (Read 8397 times)

Offline lehe

  • Multiple posting newcomer
  • *
  • Posts: 35
adding library files and getting function declaration
« on: February 02, 2009, 03:12:17 pm »
Hi,
Here are two of my questions while using codeblocks:

1. Is there a convenient way to add multiple library files for the same library, instead of adding one file at one time? I known in terminal, we can use something like "gcc `pkg-config --cflags --libs libraryname` -o my-prgm my-prgm.c".

2. While using some libraries,  I often want to see declarations of their functions.  For some of them, I right-click and try jumping to their declarations but nothing is found, even if the include directories have been correctly specified and the header files have been successfully found by the compiler. Also placing the mouse on functions sometimes don't give declaration. What should I do?

Thanks a lot!

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7252
Re: adding library files and getting function declaration
« Reply #1 on: February 02, 2009, 03:26:15 pm »
Code
`pkg-config --libs libraryname`
can be used if pkg-config and an appropriate config-file exists.

Just place it including the backticks in your projects build options, tab "Linker settings -> Other linker options".

Code
`pkg-config --cflags libraryname`
can be used for includes and compile-flags in "Compiler settings -> Other options".

The backticks work on windows also (only in C::B).

You can, of course, call any other external program or script also and the call within the backticks will be replaced by the output.

Offline lehe

  • Multiple posting newcomer
  • *
  • Posts: 35
Re: adding library files and getting function declaration
« Reply #2 on: February 03, 2009, 01:08:47 am »
Thanks, Jens! I tried what you said and it works!

I still feel very hard to get declarations of functions of the libraries I am using.   I cannot get the declarations by right-clicking and choosing to jump to their declarations in the header files or by placing the mouse on their name to get a hint. However in Visual C++, I can get declarations of the functions in either way. I think I must miss something in CB. Please help if you have any idea. I am using svn5432 by the way. Thanks a lot!

some other updates:
While "gcc `pkg-config --cflags --libs opencv` `pkg-config --cflags --libs gtk+-2.0` -o my-prgm my-prgm.c" works fine, building my program with same settings in CB can give erros like these:

1.   /usr/lib/libgtk.a and /usr/lib/libgdk.a are not found. gtk+-2.0 is installed on my system, but the missing library files are provided by a lower gtk version gtk-1.2 and the problem can be fixed by installing gtk-1.2.  Is this because codeblocks is based on gtk-1.2 or something?

2.
Code
/usr/lib/libX11.a(CrGlCur.o)||In function `open_library':|
||warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking|
/usr/lib/libdl.a(dlopen.o)||In function `dlopen':|
(.text+0x1b)||undefined reference to `__dlopen'|
/usr/lib/libdl.a(dlsym.o)||In function `dlsym':|
(.text+0x1b)||undefined reference to `__dlsym'|
||=== Build finished: 2 errors, 1 warnings ===|
I fix it by adding libdl.a to linking.  Is it because codeblocks is based on libdl or some other reason?

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9723
Re: adding library files and getting function declaration
« Reply #3 on: February 03, 2009, 12:17:16 pm »
I fix it by adding libdl.a to linking.  Is it because codeblocks is based on libdl or some other reason?
Note: This is a linker error that is just reported by C::B (which is an IDE, not a compiler/linker). So it's not a C::B issue.

In general: If you use function foo of library bar you need to link against the library bar. That's how libraries work: They provide functions you don't need to implement yourself but you are responsible for linking the libraries into you application correctly. Grab a programmers book to read more about this topic. It's really helpful if a developer knows at least how a compiler/linker works and how I use 3rd party libraries correctly.

However - this goes beyond the scope of C::B and thus violates our forum rules. Topic locked therefore.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9723
Re: adding library files and getting function declaration
« Reply #4 on: February 03, 2009, 04:15:07 pm »
Topic re-opened on request with more details (now it's C::B specific):

Quote
1. My questions is: in CB, even if I set correctly the directories of header files and library files and what the library files that my code should be linked to and build my program successfully, I still can't get the declaration for its functions or jump to its header files in CB, neither can I for some functions of standard C/C++ library, like exit(), printf(). But I can get declarations from string or math libraries, like for strcp() and log(). Without being able to see the declaration of functions I need, I feel very difficult to use CB. I guess I might be doing something wrong, but it is really not a how-to-use-library issue, but a how-to-use-CB issue.  I have been bothered by this problem for quite some time, along with 8.02, 5382 and 5432. If someone know a clue, it will help me a lot.

2. for CB and gcc, under the same setting "`pkg-config --cflags --libs opencv` `pkg-config --cflags --libs gtk+-2.0`" , my program could be built successfully with gcc but not CB unless I have to do some extra stuff: install gtk1.2 and link my program to libdl.a.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7252
Re: adding library files and getting function declaration
« Reply #5 on: February 03, 2009, 04:31:52 pm »
Code
`pkg-config --libs libraryname`
can be used if pkg-config and an appropriate config-file exists.

Just place it including the backticks in your projects build options, tab "Linker settings -> Other linker options".

Code
`pkg-config --cflags libraryname`
can be used for includes and compile-flags in "Compiler settings -> Other options".

The backticks work on windows also (only in C::B).

You can, of course, call any other external program or script also and the call within the backticks will be replaced by the output.

Did you follow my steps ?
The first one is important.
C::B normally divides compiling and linking in two steps.

Compiling needs the --cflags-parameter for pkg-config, linking needs --libs.

Offline lehe

  • Multiple posting newcomer
  • *
  • Posts: 35
Re: adding library files and getting function declaration
« Reply #6 on: February 03, 2009, 04:55:35 pm »
Hi Jens,
Your steps work fine! It's just that, compared to gcc in terminal, I have to do some extra things, besides  "`pkg-config --cflags --libs opencv` `pkg-config --cflags --libs gtk+-2.0`",  to build my program in CB : install gtk1.2 and link my program to libdl.a. Although not clear about why, I've fixed this kind of problem.

The other problem, which have been bothered me since I first used CB along with 8.02, 5382 and 5432, is: getting the declaration of functions or variables in CB sometimes work sometimes not.
(1) For library functions, even if I set correctly the directories of header files and library files and what the library files that my code should be linked to and build my program successfully, I still can't get the declaration for its functions or jump to its header files in CB, neither can I for some functions of standard C/C++ library, like exit(), printf(). But I can get declarations from string or math libraries, like for strcpy() and log().
(2) For self-written functions or variables, I can jump to their declarations, but I cannot get them simply by placing mouse on them.
(3) The mouse can get declaration for A when it is defined by "#define A B", but the declaration I get is "AA B" (two A's in a row), something messed up.

If someone knows any clue, it will help me a lot.

Thank you!

Update:
I searched the forum a little bit. The declaration problem is not unusual. I tried two suggestions posted: in search directory add the one for header files, and reparse the symbols. But they don't solve the problem. Are you guys not bothered by this problem? How do you get around it? Thanks!
« Last Edit: February 03, 2009, 10:12:40 pm by lehe »