0

I have a program in C (program_a.c) this program depends some functions that it doesn't have declared in its body, because this functions are in another file (library.h), but I don't have the file .h or file .c only I have the version compilated which is a binary file.

Can I compile my program and make use of this library that isn´t a .h or .c file?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • I would not be too optimistic that you can get this to work. See this question for details: https://stackoverflow.com/questions/16197109/generate-header-file-h-for-on-shared-library-file-so – bruceg May 22 '19 at 00:34

2 Answers2

1

If you know the functions in the library you can make your own library.h that will allow the compiler to succeed and then you just need the linker to ensure library.o (or .a or...) is linked in.

John3136
  • 28,809
  • 4
  • 51
  • 69
  • Thanks for the answer, I have only the libprofile.a file wich is the compilated library, seraching in the server I found the libprofile.h file, but its incomplete because it has only two structs declarated, but it doesn´t have the functions that the program leemsgs.c needs, I tried to compile it with this line cc leemsgs.c -L. -llibprofile -o leemsgs but this is the error "leemsgs.c", line 270: warning: implicit function declaration: ReadConfig. Then I need the complete version of libprofile.h file where this and others functions are declarated? – Abraham Macias May 22 '19 at 00:12
  • @AbrahamMacias If you already have the program just provide the adequate prototypes yourself from the functions and it will work provided you link correctly to the library – Iharob Al Asimi May 22 '19 at 00:27
  • Thanks John, I can understand now. – Abraham Macias May 22 '19 at 22:43
1

If your program is using a function func() from this external compiled library then you'd need to have:

  1. func() declaration - typically it will be in a header file, (which typically is provided with the library). If this file is called profile.h you'd #include it from the source code in your program that is calling func().
  2. func() definition - the implementation of this function - although you cannot see it, it must be in the binary of this library. You can verify it by running the nm libprofile.a which will show you the library's functions names (although sometimes it might not display them if its symbols were stripped when compiled).

If you don't have the declaration - your code wouldn't compile.

If you don't have the definition - your code wouldn't link.

SHG
  • 2,516
  • 1
  • 14
  • 20
  • Thanks for the answer, Then I need both things the header file for put it in the code by a #include, and the implementation in this case is in the profile.a file, this can´t work only with the profile.a file, too is necesary put the #include "profile.h"? – Abraham Macias May 22 '19 at 18:38
  • Yes, including the header file is necessary. As @John3136 mentioned in his answer, you could create the header file yourself if you knew the functions declarations of the library functions you use in your your code, but typically you should receive this info with the binary of the libarary in a form of a header file. – SHG May 22 '19 at 18:48
  • 1
    That is the problem, they don't have the profile.h file complete, and I can't suppose how the functions of this library works because I dont know the functions declarations, I need the complete code to understand how does it works. Well thank you very much to you and @John3136. – Abraham Macias May 22 '19 at 22:41