Creating and use a static library with g++
There was a week I'm learning and reading how to use my own stat开发者_Go百科ic library in C++ and it's driving me crazy. I'm using Ubuntu and I have a class in /home/myFiles/lib I want to build my library:
//MyClass.h
//---------------------------------------------
#ifndef MYCLASS_H_
#define MYCLASS_H_
class MyClass {
public:
int a;
MyClass();
virtual ~MyClass();
};
#endif
And
//MyClass.cpp
//------------------------------------------
#include "MyClass.h"
MyClass::MyClass() {
a = 3;
}
MyClass::~MyClass() {
}
//------------------------------------------
And a test file in /home/myFiles/main where I want to use the library:
// test.cpp
//-------------------------------------------------
#include <iostream>
using namespace std;
#include "MyClass.h"
int main() {
MyClass c = MyClass;
cout << "Hello World!!!" << c.a << endl;
return 0;
}
//----------------------------------------------
Using Ubuntu console I type:
In lib folder: g++ -c MyClass.cpp
ar rsc libTesting.a MyClass.o
In test folder: g++ -c test.cpp -lTesting -L/home/myFiles/lib
Then It returns: test.cpp: error: MyClass.h: No such file or directory
If I write:
g++ -o test.cpp -lTesting -L/home/myFiles/lib
It deletes my file test.cpp
I know there is a lot of information about this but I don't understand why it don't recognize the #include "MyClass.h"
and how can I use this using only the library without include the path with -I/home/myFiles/lib
.
Thank you.
you havn't indicate the include path, so gnu report the error.
try to add -I for your include path, or copy the .h file to your source directory.
精彩评论