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.
-loption for specifying libraries is ignored by the preprocesser (it's used by the linker, a few steps after processing#includedirectives). 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.-Ioption.