I am trying to create and link a static library in C++. I have two files:
helloWorld.cpp
#include <iostream>
int main(int argc, char const *argv[]) {
std::cout << "I'm inside main." << '\n';
printHello();
return 0;
}
libraryHello.cpp
#include <iostream>
void printHello() {
std::cout << "Hello" << '\n';
}
I am running such commands (based on http://www.techytalk.info/c-cplusplus-library-programming-on-linux-part-one-static-libraries/):
g++ -Wall -c libraryHello.cpp -o libraryHello.o
ar rcs libmylib.a libraryHello.o
g++ -static helloWorld.cpp -L. -lmylib -o helloExecute
First two go well, when trying to compile main file such error appears:
helloWorld.cpp: In function ‘int main(int, const char**)’:
helloWorld.cpp:5:14: error: ‘printHello’ was not declared in this scope
It looks as though it is not loaded at all and it cannot find printHello. Is there anything I am doing wrong when compiling, linking or anything else? What I want to do is to call printHello() procedure from main using static library and linking it to helloWorld.cpp.
My compiler: g++ 5.4.0, OS: Ubuntu 16.04 32bit