1

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.

UserX
  • 187
  • 1
  • 7
  • 2
    Do you declare e.g. `isPrimeAKS`? You do not show any forward declarations or header files in your question. – Brian61354270 Mar 15 '20 at 19:49
  • 1
    please provide a [mre] – Alan Birtles Mar 15 '20 at 20:00
  • 2
    `g++ -o main main.cpp -L.` is missing the library. `lib.so` isn't the best name though. If you link with `x` (`-lx`) it will search for a file named `libx.so` or `libx.a`. – Ted Lyngmo Mar 15 '20 at 20:00
  • Oh man, I always forget the header. Now, after adding the header for the declarations, I have a novel problem, I get main.cpp:(.text+0xe0): undefined reference to `isPrimeAKS(int)' which I think is because the compiler references to the uninitialized declaration instead of the function in the dynamic library. Editing – UserX Mar 15 '20 at 20:03
  • @UserX They are undefined because you haven't linked with your library. – Ted Lyngmo Mar 15 '20 at 20:04
  • Doesn't the . on the -L search the current directory for .so files? – UserX Mar 15 '20 at 20:05
  • 1
    Yes, but you don't supply any library to look for. Look at my first comment. You must supply a library with `-l` but the name you've chosen isn't the best. Say you named your library `libfoo.so`. You must then supply `-lfoo` when compiling. – Ted Lyngmo Mar 15 '20 at 20:07
  • 1
    Ohhh it ignores ^(lib).*(.so)$, I couldn't pick a worse name. That makes sense. – UserX Mar 15 '20 at 20:11

1 Answers1

6

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.

Jérôme Richard
  • 41,678
  • 6
  • 29
  • 59