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

Corey Farrell git at cfware.com
Tue Dec 20 09:46:39 CST 2016


Forget about *.exports if your goal is to make system.c functions available
for use from chan_sip.c.  All that is needed is for system.c to be placed
in channels/sip/ - not channels/.  Putting the file in the correct place
will link it into chan_sip.so, thus making the functions available to call
from chan_sip.c.  If this does not resolve your issues you will need to
post your patched asterisk to github.com or similar world readable GIT
repository - we can't see what you're doing wrong unless you show us.

On Sat, Dec 17, 2016 at 1:31 AM, Julian Fleischhauer <
julian.fleischhauer at stud.uni-due.de> wrote:

> Because this problem most likely being the last barrier to finish my
> project, I will explain it once again in a little more detail.
>
> There are the following files that I think may be important to solve the
> problem:
> *channels/chan_sip.c*
> *channels/sip/include/system.h*
> *channels/system.c*
> *channels/system.exports.in <http://system.exports.in>*
>
> I want *system.c* to export several functions, one of them is
> *sys_system_get_our_address(struct ast_sockaddr *us).*
>
> That function is implemented in *system.c, *gets called in *chan_sip.c*
> and it's prototype is declared in *system.h*.
>
> To export that function, I created the file system.exports.in in
> channels/, which content is the following:
> {
> global:
> LINKER_SYMBOL_PREFIXsys_*;
> LINKER_SYMBOL_PREFIX_IO_stdin_used;
> local:
> *;
> };
>
> I tried a *make dist-clean* and a complete recompilation afterwards. The
> build system doesn't create the file *system.exports*, but it creates
> *chan_sip.exports* which is in the same folder. I dig into to the system
> and found *build-tools/make_linker_version_script* which probably creates
> the exports files.
> test -f ${1}.exports.in && ${AWK} "{sub(\"LINKER_SYMBOL_PREFIX\",
> \"${2}\"); print;}" ${1}.exports.in > ${1}.exports && exit 0
> test -f ${1}.exports.in || rm -f ${1}.exports && cp ${3}/default.exports
> ${1}.exports && exit 0
>
>
> Can anyone see any mistakes I make? Thank you very much.
>
> Regards,
> Julian Fleischhauer
>
>
> 2016-12-12 11:40 GMT+01:00 Julian Fleischhauer <
> julian.fleischhauer at stud.uni-due.de>:
>
>> Thank you for this simple solution, it solved the last problem.
>>
>> Another issue arised: Before, system.c was in the main/ directory and
>> used the project wide exports file to export its functions, now it's in
>> channels/ where apparantly doesn't exist such a file.
>> I created *system.exports.in <http://system.exports.in>* in channels/ to
>> export the functions but the Asterisk build system does not automatically
>> generate the *system.exports* file and manually creating it doesn't work
>> either.
>>
>> *system.h*
>> void ast_system_get_address(struct ast_sockaddr *us);
>>
>> *system.c*
>> #include "asterisk/system.h"
>>
>> void ast_system_get_address(struct ast_sockaddr *us)
>> {
>>    ...
>> }
>>
>> *chan_sip.c*
>> #include "asterisk/system.h"
>>
>> ast_system_get_address(us);
>>
>> 2016-12-09 18:42 GMT+01:00 Corey Farrell <git at cfware.com>:
>>
>>> I'm not sure what you're trying to accomplish but this is not the
>>> approach I'd take for expanding chan_sip.  My advice is to simply put your
>>> system.c into channels/sip.  This will automatically link it into the
>>> binary chan_sip module, but allow you to keep your source code in a
>>> separate file.  I believe MODULEINFO (for any added dependencies) and
>>> possibly a call to your init/cleanup would be the only changes you'd need
>>> to make in chan_sip.c.  All non-static symbols in chan_sip.c and
>>> channels/sip/*.c are available across all sources of chan_sip.so without
>>> needing chan_sip.exports.in or AST_MODFLAG_GLOBAL_SYMBOLS.
>>>
>>> The only exception is if you need to call chan_sip functions from
>>> another existing module, then you will have to export the symbols.  To do
>>> this you have to tell the linker (via chan_sip.exports.in) and you have
>>> to tell the module loader.  AST_MODULE_INFO at the very end of chan_sip.c
>>> would need AST_MODFLAG_GLOBAL_SYMBOLS so the module loader knows to share
>>> the globals:
>>>
>>> AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS
>>> | AST_MODFLAG_LOAD_ORDER, "Session Initiation Protocol (SIP)",
>>>
>>>
>>>
>>> On Fri, Dec 9, 2016 at 10:50 AM, Julian Fleischhauer <
>>> julian.fleischhauer at stud.uni-due.de> wrote:
>>>
>>>> I could resolve the XML and pcap issues, but still have problems
>>>> concerning "sip_get_header()". I don't want to rename the function since
>>>> it's a frequently used function in chan_sip.c, I therefore defined an .
>>>> exports.in file. I don't know if Mr. Michelson understood me
>>>> correctly, but I think chan_sip.c exports "sip_get_header()" (doesn't it?)
>>>> so I modified *chan_sip.exports.in <http://chan_sip.exports.in>* so it
>>>> looks like
>>>> {
>>>> global:
>>>> LINKER_SYMBOL_PREFIXsip_get_header;            // I also tried
>>>> LINKER_SYMBOL_PREFIXsip_*;
>>>> LINKER_SYMBOL_PREFIX_IO_stdin_used;
>>>> local:
>>>> *;
>>>> };
>>>> But it doesn't work. I did the same with *system.exports.in
>>>> <http://system.exports.in>*, without success.
>>>>
>>>>
>>>> Sorry for the badly formatted last mail. I will post the updated file
>>>> structure again 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*
>>>> #include "asterisk/system.h"
>>>> #include "asterisk/sip.h"
>>>>
>>>> void ast_some_function(struct sip_pvt *p, struct ast_sockaddr *addr)
>>>> {
>>>>    ast_copy_string(touser, sip_get_header(req, "To"),
>>>> strlen(sip_get_header(req, "To")) + 1);
>>>> }
>>>>
>>>> *chan_sip.c*
>>>> #include "asterisk/system.h"
>>>> #include "asterisk/sip.h"
>>>>
>>>> ast_some_function(p, addr);
>>>>
>>>>
>>>> Best regards,
>>>> Julian Fleischhauer
>>>>
>>>> 2016-12-06 18:35 GMT+01:00 Mark Michelson <mmichelson at digium.com>:
>>>>
>>>>> 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.
>>>>>
>>>>>
>>>>> --
>>>>> _____________________________________________________________________
>>>>> -- Bandwidth and Colocation Provided by http://www.api-digital.com --
>>>>>
>>>>> asterisk-dev mailing list
>>>>> To UNSUBSCRIBE or update options visit:
>>>>>   http://lists.digium.com/mailman/listinfo/asterisk-dev
>>>>>
>>>>>
>>>>
>>>> --
>>>> _____________________________________________________________________
>>>> -- Bandwidth and Colocation Provided by http://www.api-digital.com --
>>>>
>>>> asterisk-dev mailing list
>>>> To UNSUBSCRIBE or update options visit:
>>>>    http://lists.digium.com/mailman/listinfo/asterisk-dev
>>>>
>>>
>>>
>>> --
>>> _____________________________________________________________________
>>> -- Bandwidth and Colocation Provided by http://www.api-digital.com --
>>>
>>> asterisk-dev mailing list
>>> To UNSUBSCRIBE or update options visit:
>>>    http://lists.digium.com/mailman/listinfo/asterisk-dev
>>>
>>
>>
>
> --
> _____________________________________________________________________
> -- Bandwidth and Colocation Provided by http://www.api-digital.com --
>
> asterisk-dev mailing list
> To UNSUBSCRIBE or update options visit:
>    http://lists.digium.com/mailman/listinfo/asterisk-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-dev/attachments/20161220/7ccfb843/attachment-0001.html>


More information about the asterisk-dev mailing list