0

I have created C++ static library using object files (which has many sub directories). After creating static library created a C file and a wrapper file (header file which I have under C++ directories). Now I'm trying to compile the C file by linking the C++ static library it gives error for the header file

error : no such a file or directory.

When I use -I option (-I C++ header files location) compiled successfully and able to run. But I would like to use the static library without including header files location i.e only adding static library itself C program should compile successfully.

Below is the source:

Edit:

I have the below files under libbasic folder:

testdemo.h

#ifndef TESTDEMO_H #define TESTDEMO_H #include<iostream> #include<string> using namespace std; class testdemo { public: testdemo(); void CallingTestDemo(); }; #endif // TESTDEMO_H 

testdemo.cpp

 #include "testdemo.h" testdemo::testdemo() { } void testdemo::CallingTestDemo() { `cout <<" CallingTestDemo!!!!!!\n"; } 

testbasic.h

#ifndef LIBBASIC_H #define LIBBASIC_H #ifdef __cplusplus #include<iostream> #include<testdemo.h> using namespace std; class Libbasic { public: Libbasic(); void Display(); void DisplayName(char* name); }; #endif #ifdef __cplusplus extern "C" { void displayfromC(); void displayfromCName(char* name); } 

libbasic.cpp

#include "libbasic.h" void displayfromC() { Libbasic llb; llb.Display(); } void displayfromCName(char* name) { Libbasic lb; lb.DisplayName(name); } Libbasic::Libbasic() { } void Libbasic::Display() { cout <<" C called C++ API \n"; testdemo td; td.CallingTestDemo(); } #endif #endif // LIBBASIC_H 

I compiled the above program and created library libbasic.a

Now Im creating C API file outside the libbasic folder to call the above functions used in C++

testApi.c

#include<stdio.h> #include <libbasic.h> int main() { displayfromC(); } 

Now im trying to create output using the below

gcc -o testdemo testApi.c -L ./libbasic -lbasic

Which giving libbasic.h: no such a file or directory error.

The basic Idea is create a library and API functions which can be used in any machine. If I have multiple folders and header files in C++ code then need to include all the folders while creating C application which requires to export header files too. I dont want to expose all the source to other users.

Kindly let me what mistake im doing and also how to achieve this.

4
  • 2
    the headers and the library are separate, you need them both Commented Nov 13, 2020 at 17:25
  • Unfortunately, nobody can let you know anything without a minimal reproducible example, which you should've included in your question after taking a tour, reading the help center, and learning How to Ask questions, first, before asking. Commented Nov 13, 2020 at 17:38
  • "it gives error for the header file" -- this implies that you are including the header files for your library. How is the preprocesser supposed to find them? The -l option for specifying libraries is ignored by the preprocesser (it's used by the linker, a few steps after processing #include directives). Not to mention that when you compiled your library, the locations of its header files were dropped during its preprocessing; the final library file does not know where its headers were. Commented Nov 13, 2020 at 19:47
  • You might want to back up a step (c.f. XY problem) and explain why you do not want to use the -I option. Commented Nov 13, 2020 at 19:53

1 Answer 1

3

A header file is not a wrapper.

If you have functions in a library, whether static or dynamic, you must have their declarations (often called "prototypes") in your code.

That is how C and C++ work so header files are integral part of any library.

As for the location of header files, these languages give you three options:

  1. Use <filename.h> for standard include header path. This path depends on your OS and compiler, but as long as your header file is there, you will not need to specify the path explicitly in compilation command.

  2. Use "filename.h" for files in your local source directory, i.e. same directory where your *.c or *.cpp files reside.

The compiler can look there automatically as well, but if the header is in a sub directory, you will need to specify the relative path in your include statement.

#include "mylib/myheader.h" 
  1. Add additional search path for header files using a parameter (such as -I for gcc).

Finally, similar rule applies to the library object file it self (*.a, *.lib).

You will need the -L parameter in addition to the -l parameter if your library is not in a standard library path known to the linker.

Note that you never specified which compiler you are using, so I am guessing based on parameter names it is from gcc family or similar.

A way to alter the search path and tell the compiler where to find your library files will differ based on compiler type, but the principle is the same.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.