Q:

Getting Undefined Reference to Perl on C++

Well, first of all, sorry about my bad english!
I'm new to linux, g++ and perl, and I'm getting some problems here.
I have a code in G++ which calls a perl .pl file to return a information. Right now, I'm just returning 1 or 0 from the perl .pl file for tests and to understand how does it works. But the problem is that I'm getting this from the $make:
sathlervbn Spam C # make clean;make
rm -f *.o
g++  -Wall  -D_REENTRANT -D_GNU_SOURCE -DDEBIAN -fstack-protector -fno-strict-aliasing -pipe -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64  -I/usr/lib/perl/5.14/CORE   -c -o filedir.o filedir.cpp
g++  -Wall  -D_REENTRANT -D_GNU_SOURCE -DDEBIAN -fstack-protector -fno-strict-aliasing -pipe -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64  -I/usr/lib/perl/5.14/CORE   -c -o main.o main.cpp
main.cpp: In function ‘int main(int, char**, char**)’:
main.cpp:112:41: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
main.cpp:112:41: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
g++ -L/usr/lib -Wall -Wl,-E  -fstack-protector -L/usr/local/lib  -L/usr/lib/perl/5.14/CORE -    lperl -ldl -lm -lpthread -lc -lcrypt -o main filedir.o main.o
main.o: In function `getInfoPerl(std::string)':
main.cpp:(.text+0x1a): undefined reference to `Perl_push_scope'
main.cpp:(.text+0x33): undefined reference to `Perl_save_int'
main.cpp:(.text+0x73): undefined reference to `Perl_markstack_grow'
main.cpp:(.text+0xcd): undefined reference to `Perl_stack_grow'
main.cpp:(.text+0xfa): undefined reference to `Perl_newSVpv'
main.cpp:(.text+0x10d): undefined reference to `Perl_sv_2mortal'
main.cpp:(.text+0x13b): undefined reference to `Perl_call_pv'
main.cpp:(.text+0x18f): undefined reference to `Perl_sv_2iv_flags'
main.cpp:(.text+0x1bd): undefined reference to `Perl_free_tmps'
main.cpp:(.text+0x1ca): undefined reference to `Perl_pop_scope'
main.o: In function `main':
main.cpp:(.text+0x206): undefined reference to `Perl_sys_init3'
main.cpp:(.text+0x20b): undefined reference to `perl_alloc'
main.cpp:(.text+0x21d): undefined reference to `perl_construct'
main.cpp:(.text+0x265): undefined reference to `perl_parse'
main.cpp:(.text+0x272): undefined reference to `perl_run'
main.cpp:(.text+0x2fd): undefined reference to `perl_destruct'
main.cpp:(.text+0x30a): undefined reference to `perl_free'
main.cpp:(.text+0x30f): undefined reference to `Perl_sys_term'
collect2: error: ld returned 1 exit status
make: *** [main] Error 1

The main.cpp code is:
#include <EXTERN.h>
#include <perl.h>
#include <iostream>
#include <cstdio>
#include "filedir.h"

using namespace std;  

PerlInterpreter *my_perl;

int getInfoPerl(string email){
    dSP;
    ENTER;
    SAVETMPS;
    PUSHMARK(SP);
    XPUSHs(sv_2mortal(newSVpv(email.c_str(),0)));
    PUTBACK;
    call_pv("spamTeste", G_SCALAR);
    SPAGAIN;

    int resultado = POPi;
    PUTBACK;
    FREETMPS;
    LEAVE;

    return resultado;

}

    int main(int argc, char **argv, char **env) {

    char *my_argv[] = { " ", "spamPerl.pl" };
    PERL_SYS_INIT3 (&argc, &argv, &env);

    my_perl = perl_alloc();
    perl_construct ( my_perl );
    PL_exit_flags |= PERL_EXIT_DESTRUCT_END;

    perl_parse(my_perl, NULL, 2, my_argv, (char **)NULL);
    perl_run(my_perl);

    cout << "Resultado " << getInfoPerl("email/email.txt") << endl;

    perl_destruct(my_perl);
    perl_free(my_perl);

    PERL_SYS_TERM();

    foobar bla bla bla another part from the code: doesn't matter.
}

Here is the makefile:
#CC= /usr/bin/g++
CPP = g++
CPPFLAGS = -Wall $(shell perl -MExtUtils::Embed -e ccopts)
#LD= /usr/bin/g++
LD = g++

LFLAGS = -Wall $(shell perl -MExtUtils::Embed -e ldopts)

#LFLAGS = -Wall -Wl,-E  -fstack-protector -L/usr/local/lib  -L/usr/lib/perl/5.14/CORE -lperl     -ldl -lm -lpthread -lc -lcrypt

MAINOBJS = filedir.o main.o

EMAILS = main

EXECS = $(EMAILS)

#Regra Implicita:
.c.o:
    $(CPP) $(CPPFLAGS) -c $<

all: emails

emails: $(EMAILS)

main: $(MAINOBJS)
    $(LD) -L/usr/lib $(LFLAGS) -o $@ $(MAINOBJS)

clean:
    rm -f *.o

What I did? I've tried installing libperl-dev package, update the perl, and nothing solved. 
I really need to fix this! Can someone help me?
UPDATE:
Changed the Header from main.cpp to:
#ifdef __cplusplus
extern "C" {
#endif
#include "EXTERN.h"
#include "perl.h"
#ifdef __cplusplus
}
#endif

#include <iostream>
#include <cstdio>
#include "filedir.h"

Didn't work...

A:

The problem was in the makefile:
#CC= /usr/bin/g++
CPP = g++
CPPFLAGS = -Wall $(shell perl -MExtUtils::Embed -e ccopts)
#LD= /usr/bin/g++
LD = g++

LFLAGS = -Wall $(shell perl -MExtUtils::Embed -e ldopts)

#LFLAGS = -Wall -Wl,-E  -fstack-protector -L/usr/local/lib  -L/usr/lib/perl/5.14/CORE -lperl     -ldl -lm -lpthread -lc -lcrypt

MAINOBJS = filedir.o main.o

EMAILS = main

EXECS = $(EMAILS)

#Regra Implicita:
.c.o:
$(CPP) $(CPPFLAGS) -c $<

all: emails

emails: $(EMAILS)

main: $(MAINOBJS)
    $(LD) -L/usr/lib $(LFLAGS) -o $@ $(MAINOBJS)

clean:
    rm -f *.o

As you can see, in this line, the code:
main: $(MAINOBJS)
    $(LD) -L/usr/lib $(LFLAGS) -o $@ $(MAINOBJS)

Should have the $(LFLAGS) after the $(MAINOBJS), so it should be:
main: $(MAINOBJS)
    $(LD) -L/usr/lib -o $@ $(MAINOBJS) $(LFLAGS)

Now, the linker is working perfectly. I'm sorry, but I can't say exactly why this is necessary, I've just discovered.

