I have compiled a hello world library to test my build setup:
// hw.cpp
#include<iostream>
#include"hw.h"
void hw(){ std::cout << "hw"<<std::endl; }
// hw.h
void hw();
// main.cpp
#include"hw.h"
int main(int argv, char ** argc){
hw();
return 0;
}
// BUILD COMMAND (ommitting proj directories, replacing with '.')
g++ -I. -o hw.o hw.cpp
// Archive Command
ar -rcs hw.a hw.o
// Compile (works as expected)
g++ -I. -o hw main.cpp hw.a
// Compile with ld
g++ -I. -L. -o hw main.cpp -lhw
And the result is failure. I have tried absolute paths, looked at ld documentation; and in general burnt time trying to figure out this issue.
What could be going on here?
Exact error code:
/usr/bin/ld: cannot find -lhw
collect2: error: ld returned 1 exit status
I'm guessing your error message, but after trying your code myself, I think that your problem is that ld cannot find your library. Libraries have to be named with the prefix lib
. Rename hw.a
to libhw.a
and it should work.