Can't include a static lib(.lib) in visual c++ - c

I've added a .lib to my c project,
but still can't use things defined in that .lib in .c
main()
{
structure_defined_inthat_lib c;
...
I added that .lib this way:
Right click ,property,Linker,Input,
and in "Additional Dependencies",I type in "D:\path\name.lib"
Anything wrong?

You need to include the header files defining the stuff in that library into the code where you want to use that stuff. That header file must be on one of the paths specified in C++->General->Additional include directories - you might need to add the path to the header to that list.
Also you only specify the filename of the library in "Additional dependencies" in the linker settings and provide the path to that file in "Additional library directories".

You need both the library and the header to use the functions defined in the library. If you know the function signatures, you can also declare them using extern.

Related

How to solve "Undefined reference to function" in Eclipse CDT?

I did setup a C project with Eclipse Photon (4.8.0) for developing a program for the ESP-32. I did configure the IDE according to this official setup instructions.
Flashing the ESP-32 works fine. But as soon as I try to include header files from a sub folder, I run into troubles. I have set up a very simple project to illustrate the issue. The project consists of main.c, base/test.h and base/test.c, whereas the test.h and test.c files only contain one function with the signature void function1(void);.
When I try to call function1() in main.c, I get this error in main.c:
Undefined reference to function1()
Please compare to the attached screenshot, where everything is depicted.
How to solve this issue?
This is not a compiler, but rather a linker error.
Note, with #includeing a header file, you only make the external function known to the compiler. You also need to link to the external function during the linking stage. Make sure you include the compiled object file that contains function1 into the link.
Seems like you need to do proper linking.
If you are linking with a library, you need to specify:
The name of the library: Project\Settings\C C++ General\Paths and Symbols\Libraries
Location where the linker should search for this library:
Project\Settings\C C++ General\Paths and Symbols\Library Paths
Important: see Note.
If you are linking with object files, add those to:
Project\Settings\C C++ Build\Settings\Linker\Miscellaneous\Other objects
Note:
If your library name is, for example, libsomething.a, than you need to specify only something as the name; so omit lib prefix and .a suffix.
If your library is not prefixed with lib, then you need to add its name prefixed with :. For example, something.a should be added as :something.a.

How to add .h and .o files to gcc

I'm trying to figure out how to add header and object files to the standard library so that I can use my custom files as easy as the standard library.
Currently I have to type the path to the header file in the .c file and then link the object file path when compiling.
I would like to just be able to add:
#include <mystdlib.h>
and not worry about linking the object file like I do when I reference the stdio.h header file.
I have searched around, but I fear I'm not using the proper terminology as I don't seem to find the results I need. Am I the first to want to do this, or it is just impossible, and therefore people don't even try?
gcc uses environment variables C_INCLUDE_PATH and LIBRARY_PATH to look for header and library files. Setting them somewhere (eg., your bash_profile) should achieve what you describe:
export C_INCLUDE_PATH="$HOME/include"
export LIBRARY_PATH="$HOME/lib"
Alternatively, the -I and -L flags add directories to the list of directories to be searched for header files and library files, respectively.
edit: As noted by #ChrisStratton below, the library name or object file needs to be explicitly specified. AFAIK, there is no way to make gcc always link against a library (like with libc).
sources:
https://gcc.gnu.org/onlinedocs/gcc/Environment-Variables.html
https://gcc.gnu.org/onlinedocs/gcc/Directory-Options.html

Undefined reference in C with gcrypt.h

I'm trying to use the library gcrypt.h but show this error:
undefined reference to `gcry_md_get_algo_dlen'
The code is:
int algo = GCRY_MD_SHA1;
unsigned int hash_len = gcry_md_get_algo_dlen(algo);
unsigned char hash[hash_len];
How can I fix it?
Make sure you have the most recent version of the library http://www.gnupg.org/download/#libgcrypt
If you have the right version make sure you added the library itself to your linker settings in Eclipse.
To do so:
Right-Click on your project -> Properties / C/C++Build / Settings / GCC C++ Linker / Libraries
There you add to libraries "gcrypt" ( you don't at the "lib" to it )
And also make sure that ( if the lib isnt under a system path ) you add the path where the library itself lies.
It looks like you are facing a Linking Error (Undefined reference to a function)
You have included the header "gcrypt.h" but the object file is not linked to your main file.
Library is NOT the same as the header (.h) file. C libraries are collections of compiled objects which are LINKED to your object code by the linker. Header files are lexically included by the preprocessor.
When you compile, you need to make sure that the libraries are where they're supposed to be AND the header files are where they're supposed to be. Either one can mess you up. Make sure that .o files are linked properly

How do I locate a C library header file from a Makefile.PL?

How can I locate a C library header file from a Makefile.PL?
There is ExtUtils::Liblist to find libraries, but I can't see the equivalent for header files.
Devel::CheckLib is what most authors seem to be use for that. It checks both for the existance of libraries as well as header files, as those libraries are probably useless without them.
Why not write a small lib to do this (and let it live in inc/lib/FindHeaders.pm or some such under the top level of your package dir). Just use Config (see incpath) along with File::Find and glob and you should be able to have a small library which can walk your default include path and map a hash of header files on your system, returning it to a call from Makefile.PL. It would be trivial to extend this to include non-standard include paths as well with a constructor argument to your lib. Easy peasy.

How to tell the preprocessor to search for a particular folder for header files, when I say #include <xyz.h>

I have around 120 header files (.h files) , and in all of them each one includes many other header files using #include <abcd/xyz.h>, but as I kept .h files in a specific folder, preprocessor is generating filenotfound error.
I moved all the .h files to the single .C file that is calling the first headerfile.
One way to do is make #include <abcd/xyz.h> as #include "abcd/xyz" , but I need to do this in all the header files wherever there is an include statement, and there are hundreds of them.
I can't include many of them in the headerfiles section in Visualstudio because, some of the headerfiles have the same name, but they reside in different directories. (<abcd/xyz.h>,<efgh/xyz.h>).
Any way to do this?
You should add a path into "Additional include directories" in the "C++" section of the project options (the "General" tab). You can use environment variables as well as "this folder" (.) shortcut and "up one folder" (..) shortcut for this setting to not be bound to a certain directory structure.
and I can't include many of them in the headerfiles section in Visualstudio because , some of the headerfiles have the same name, but they reside in different directories.(,)
That's a pretty big problem unless the files that are including those non-uniquely named headers are in the same directory as the header files themselves.
You have no way to guarantee that the compiler will locate one header before another without modifying the #include directive itself (and adding a relative path as one example).
EDIT: It looks like Visual Studio will allow you to specify different Additional Include Directories for each source file in a project (rt-click on the source file in Solution Explorer and modify C/C++ properties). But I think this would be more work than modifying the #include directives themselves - depends on how many non-unique header filenames you have.
In the project settings (under C/C++ in VS2005/2008) there's an option for "additional include directories". You can add the folders containing your header files here, using relative paths.
You can also do this at the IDE level in Tools -> Options -> Projects and Solutions -> VC++ Directories -> Include Files. Typically this method is reserved for headers included as part of a formal library. The first option is typically preferred as it's portable (you can ship your project file to another developer and, provided you use relative/macro'd paths, they can build the project as-is).
What you're looking for is the -I flag and you give the directory...
If you have a Makefile, you should add it to the CPP_FLAGS something like that....
You can also add an INCLUDE variable to your environment variables.

Resources