Search code examples
c++g++static-librariesunix-ar

Issue including library C++


I have a very simple C++ file Multiplier.cpp with the corresponding header file, from which I have created a library using the following commands

$ g++ -o Multiplier.o -c Multiplier.cpp
$ ar cr libMultiplier.a Multiplier.o

In a new directory, I have created a Calculator.cpp (with corresponding header file) which uses the Multiplier class. the header file Calculator.h has the corresponding `#include "Multiplier.h"

The header file Calculator.h has the corresponding #include "Multiplier.h"

I copied the created library into this directory I tried to compile Calculator.cpp using the following command:

$ g++ -o Calculator Calculator.cpp -L. -lMultiplier

which gives me these errors:

In file included from Calculator.cpp:1:
Calculator.h:1:24: error: Multiplier.h: No such file or directory
In file included from Calculator.cpp:1:
Calculator.h:6: error: ISO C++ forbids declaration of ‘Multiplier’ with no type
Calculator.h:6: error: expected ‘;’ before ‘&’ token
Calculator.h:8: error: expected `)' before ‘&’ token
Calculator.cpp:5: error: redefinition of ‘class something::Calculator’
Calculator.h:5: error: previous definition of ‘class something::Calculator’`

I have searched for a long time but can't work out what is happening. I am using GCC 4.2.1.


Solution

  • Your include file Multiplier.h is in another folder. How do you expect the compiler to find it? You need to tell the compiler where the file is, either using the -I command line flag and supplying the path of the include file, or using a path in the #include directive.