[asterisk-dev] Problems with custom C-file: undefined reference

Mark Michelson mmichelson at digium.com
Tue Dec 6 11:35:50 CST 2016


On 12/05/2016 04:53 PM, Julian Fleischhauer wrote:
> Hey all,
>
> I have a problem using a custom C-file. The error ouput received when 
> compiling is given below.
>
> error output
> .../system.c:1330: undefined reference to `XML_ParserCreate'
> ...
> .../system.c:1465: undefined reference to `sip_get_header'
> ...
> .../system.c:1608: undefined reference to `pcap_lookupnet'
> ...
>
>
> Before, I had all code in chan_sip.c. I want to transfer the code into 
> a separate file (system.c) now. The dependencies given below 
> MODULEINFO are customly set and worked fine when used in chan_sip.c. 
> Can anyone figure out if something else has to be added somewhere? The 
> code is quoted below:
>
> sip.h
> const char *sip_get_header(const struct sip_request *req, const char 
> *name);
>
> system.h
> #include "asterisk/netsock2.h"
>
> void ast_some_function(struct sip_pvt *p, struct ast_sockaddr *addr);
>
> system.c
> /*** MODULEINFO
>      <depend>sqlite3</depend>
>      <depend>expat</depend>
>      <depend>pcap</depend>
>     <support_level>extended</support_level>
> ***/
>
> #include "asterisk/system.h"
> #include "asterisk/sip.h"
>
> void ast_some_function(struct sip_pvt *p, struct ast_sockaddr *addr)
> {
>     xmlParser = XML_ParserCreate(NULL);
>    ...
>    ast_copy_string(touser, sip_get_header(req, "To"), 
> strlen(sip_get_header(req,       "To"))+1);
>    ...
>    pcap_lookupnet(pcapDev, &netp, &maskp, errbuf);
> }
>
> chan_sip.c
> #include "asterisk/system.h"
> #include "asterisk/sip.h"
>
> ast_some_function(p, addr);
>
>
> Thank you for any help!
>
> Regards,
> Juliannn
>
I can explain why sip_get_header() is causing a problem. In order for 
functions to be callable from other modules, they need to be explicitly 
exported. This is why you'll see files like res/res_agi.exports.in, 
which define functions that can be called globally from that module. You 
have a couple of options here:

* Define a .exports.in file for your system.c file that exports the 
sip_get_header() function.
* Change "sip_get_header()" to "ast_sip_get_header()". Assuming that 
system.c is in the main/ directory, there is a project-wide exports file 
that makes it so any function starting with "ast" can be called externally.

Regarding the XML and pcap issues, the issues are either that
* You're not #including the necessary files.
* The libraries are not being linked during compilation.

The first problem is easy to fix: just #include the right files :). The 
second problem is a bit more tricky because you'll need to dig into 
Makefiles and try to figure out how libraries get linked when Asterisk 
is built.



More information about the asterisk-dev mailing list