Reputation: 187
I have 4 separate functions in 4 different files. The functions look like this:
SieveOfEratosthenes.cpp
bool SieveOfEratosthenes(int n) {
//
}
They all have no includes. In my main.cpp program I have
//Includes
void compare(bool (*f)(int)) {
//
}
int main(int argc, char **argv) {
compare(isPrimeAKS);
compare(isPrimeModulo);
compare(isPrimeSQRT);
compare(SieveOfEratosthenes);
return 0;
}
This is my makefile:
all: main clean
main: main.cpp lib.so
g++ -o main main.cpp -L.
lib.so: EratosthenesSieve.o AKS.o ModuloAlg.o SQRTModuloAlg.o
g++ -shared -o lib.so AKS.o ModuloAlg.o SQRTModuloAlg.o EratosthenesSieve.o
EratosthenesSieve.o: EratosthenesSieve.cpp
g++ -c -Wall -fpic EratosthenesSieve.cpp
AKS.o: AKS.cpp
g++ -c -Wall -fpic AKS.cpp
ModuloAlg.o: ModuloAlg.cpp
g++ -c -Wall -fpic ModuloAlg.cpp
SQRTModuloAlg.o: ModuloAlg.cpp
g++ -c -Wall -fpic SQRTModuloAlg.cpp
clean:
rm -f *.o
Compiling errors at main(after building the shared library lib.so) with errors like:
‘isPrimeAKS’ was not declared in this scope.
I don't understand why, since they all are in the library provided to the compiler.
EDIT if you have the same problem: After declaring in a header the problem becomes
undefined reference to `isPrimeAKS(int)' see the accepted answer why.
Upvotes: 1
Views: 1245
Reputation: 50328
Firstly, the prototype of the function isPrimeAKS
must be declared in the headers included in main.cpp
or in the beginning of main.cpp
(at least before it is called).
Secondly, you should link the dynamic library at compile time or load it at runtime.
Indeed, g++ -o main main.cpp -L.
do not link lib.so
. You need to add the -lyourlib
option. However, since this option ignore the prefix lib
and the suffix .so
, you need to change the invalid name of lib.so
to something valid like libaks.so
and then link it with g++ -o main main.cpp -L. -laks
.
Upvotes: 6