Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are .lib files useless without the header files?

Tags:

c++

header

I have some .lib files, but I do not have access to the .h header files. Does this mean the .lib files are useless now?

If not, how can I use them again?

I tried using this line in my program, but it doesn't seem to be compiled into the final executable (verified with CFF Explorer).

#pragma comment(lib, "SomeLibFile.lib")

So, the only way to link .lib file is through the use of its header files? Are there any tools to recover a header file for the .lib file?

like image 557
user25101622 Avatar asked Feb 12 '12 05:02

user25101622


People also ask

Do you always need a header file?

A header file should be included only when a forward declaration would not do the job. The header file should be so designed that the order of header file inclusion is not important.

Will your program run without a header file library declared?

So, in short, the answer is yes. We can compile C program without header file.

Do static libraries need header files?

No. A library contains solely and only object files. Headers are not object files. Headers are not contained in libraries.

Do I need to include header file in source file?

Including a header file is equal to copying the content of the header file but we do not do it because it will be error-prone and it is not a good idea to copy the content of a header file in the source files, especially if we have multiple source files in a program.


2 Answers

It depends on how the .lib file code was written. If it is a c api, this is what the extern keyword is for. You could find some sort of program that would show you the function exports. Then you could declare them as extern in your code. The problem would be your data structures though.

Anyways, you don't technically have to have the header files, you could define the data structures on your own, and declare the functions with the extern keyword.

You could just link the .lib when you run your linker at the end of your compilation process.

like image 128
Jonathan Henson Avatar answered Oct 23 '22 09:10

Jonathan Henson


You can link with a .lib file by passing it on the linker command line, no #pragma is necessary.

Of course, actually using anything inside it requires knowing calling conventions, function signatures, layout of user-defined types, etc. That's all usually provided by a header file, but could also be found in documentation.

In any case, header files are not compiler-generated (well MIDL and CORBA do use machine-generated header files, but all the information in the header is still manually entered into the .idl files). And unless your technology uses a type library, the needed information is not kept with a DLL.

like image 23
Ben Voigt Avatar answered Oct 23 '22 08:10

Ben Voigt