c++ - main.c:(.text+0x30): undefined reference to `reciprocal' -



c++ - main.c:(.text+0x30): undefined reference to `reciprocal' -

i trying first programme of advance linux programming. other guys felt problem reading thread not helping me. tried methods problem still there. please help me.

here main.c

#include <stdio.h> #include "reciprocal.hpp" int main(int argc, char const *argv[]) { int i; = atoi(argv[1]); printf("the reciprocal of %d %g\n", i, reciprocal(i)); homecoming 0; }

listing reciprocal.cpp

#include <cassert> #include "reciprocal.hpp" using namespace std; double reciprocal(int i) { //i should not 0 assert(i != 0); homecoming 1.0/i; }

listing reciprocal.hpp

#ifdef _cplusplus extern "c" { #endif extern double reciprocal (int i); #ifdef _cplusplus } #endif

listing makefile:

reciprocal: main.o reciprocal.o g++ $(cflags) -o reciprocal main.o reciprocal.o main.o: main.c reciprocal.hpp gcc $(cflags) -c main.c -i ../include reciprocal.o: reciprocal.cpp reciprocal.hpp g++ $(cflags) -c reciprocal.cpp -i ../include clean: rm -f *.o reciprocal

the error is:

rajat$ create gcc -c main.c -i ../include g++ -c reciprocal.cpp -i ../include g++ -o reciprocal main.o reciprocal.o main.o: in function `main': main.c:(.text+0x30): undefined reference `reciprocal' collect2: error: ld returned 1 exit status make: *** [reciprocal] error 1

you should have __cplusplus (the proper macro definition) not _cplusplus in macro.

how debug: find undefined reciprocal in main.o:

nm main.o | grep reciprocal u reciprocal

and in reciprocal.o:

with __cplusplus:

$ nm reciprocal.o | grep reciprocal 0000000000000000 t reciprocal 0000000000000020 r _zz10reciprocale19__pretty_function__

without (with _cplusplus wrote it):

$ nm reciprocal.o | grep reciprocal 0000000000000000 t _z10reciprocali 0000000000000020 r _zz10reciprocalie19__pretty_function__

c++ c gcc

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -