How to pass compiler flags to the Asterisk compiler?

Hey all,

I want to use some mysql functions and therefore I have to pass $(mysql_config --cflags --libs) to the compiler. I found different ways in the Makefile/configure script file to do it:

  • “./configure CFLAGS=-I/usr/include/mysql LDFLAGS=-L/usr/lib/i386-linux-gnu” before make OR
  • “make ASTCFLAGS=”$(mysql_config --cflags --libs)"".
    But they both do not work. I also tried all sorts of combinations. When compiling a simple test program using “gcc -o test test.c $(mysql_config --cflags --libs)” everything works fine.

Have I done anything wrong or are there other possibilities?

Thank you in advance!

Juliannn

There’s already stuff that uses MySQL in the addons directory, can you use them as a base to achieve what you want including linking?

I think it is used to configure an Asterisk Server, but I am developing. I need to use mysql functions in code.

If I turn on “res mysql_config” in menuselect, I get no undefined reference errors but though the program gets stuck at the first mysql command mysql_init.

Any tips?

Yes, but those modules are already doing the right things in Asterisk to link MySQL so they can be used as a base.

As for getting stuck at mysql_init I’ve never used it so I don’t know, but if that is just hanging it’s probably something in the client library.

I tried out literally every configuration combination concerning mysql but I wasn’t successful. If I activate the addon res_config_mysql, there is no undefined reference error but the program gets stuck at mysql_init (I do not really know but there might be some kind of race condition against res_config_mysql’s mysql_init). If I deactivate res_config_mysql, I get an undefined reference error although the configured paths to include and lib are valid. It seems that all mysql options only affect existing mysql addons not any other parts of Asterisk or at least only make an impact if a mysql addon is activated.

The specific part which tells Asterisk to link res_config_mysql against the MySQL client would be this at the top of the file:

/*** MODULEINFO
        <depend>mysqlclient</depend>
 ***/

Adding that to yours would cause Asterisk to link the module against it, provided you are building it within the Asterisk build system. If you are building it outside then it’s up to you.

Thank you for your support. It solved my issue!

Now I have a similar problem. I want to use an XML parser. Since libxml2 doesn’t work for me, I want to try expat, but it doesn’t work either because of undefined symbol XML_ParserCreate. :unamused: I used all information I could find, but I cannot help myself.

Expat needs expat.h. It is located in /usr/include. The libraries are in /lib/i386-linux-gnu and /usr/lib/i386-linux-gnu. So I added the following flags in the Makefile:

_ASTCFLAGS+=-I/usr/include
_ASTLDFLAGS+=-L/lib/i386-linux-gnu
_ASTLDFLAGS+=-L/usr/lib/i386-linux-gnu
_ASTLDFLAGS+=-lexpat

After ./configure, compiling with NOISY-BUILD=yes (I’m using expat in chan_sip.c):

gcc -o chan_sip.o -c chan_sip.c -MD -MT chan_sip.o -MF .chan_sip.o.d -MP -pthread -I/[path-to-asterisk-source-folder]/include -I/usr/include -I/usr/include/libxml2 -pipe -Wall -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -g3 -O3 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -march=native -fPIC -DAST_MODULE=\"chan_sip\" -pthread

and

gcc -o chan_sip.so -pthread -L/lib/i386-linux-gnu -L/usr/lib/i386-linux-gnu -lexpat -lxml2 -shared -Wl,--version-script,chan_sip.exports,--warn-common chan_sip.o sip/config_parser.o sip/dialplan_functions.o sip/reqresp_parser.o sip/route.o sip/security_events.o sip/utils.o -lsqlite3

What am I doing wrong?

Thank you for any help!

Regards,
Juliannn

There is information on the wiki[1] which describes how to add dependencies to the Asterisk build system and use them.

[1] https://wiki.asterisk.org/wiki/display/AST/Build+System+Architecture#BuildSystemArchitecture-Moduleswithdependencies

Thank you for your hint!

I did what is written there, but without success.

I added:

To ./configure.ac

AST_EXT_LIB_SETUP([EXPAT], [expat])
AST_EXT_LIB_CHECK([EXPAT], [expat], [expat.h], [-lexpat])

also tried without any difference in the result:

AST_EXT_LIB_SETUP([EXPAT], [expat])
AST_EXT_LIB_CHECK([EXPAT], [expat], [expat.h], [-I/usr/include], [-L/usr/lib/i386-linux-gnu], [-lexpat])

To ./build_tools/menuselect-deps.in

EXPAT=@PBX_EXPAT@

To ./makeopts.in

EXPAT_INCLUDE=@EXPAT_INCLUDE@
EXPAT_LIB=@EXPAT_LIB@

and the dependency in chan_sip.c.

As a result, ./configure tells me:

checking for expat.h in -lexpat... no

I must somehow tell the script the location of the header file and the libs, but I really have no clue.

Thanks for any help!

Regards,
Juliannn

Your AST_EXT_LIB_CHECK is incorrect. It should be something like:

AST_EXT_LIB_CHECK([EXPAT], [expat], [], [expat.h])

Where is a function from expat that can be checked for.

Now the output of the configure script is

checking expat.h usability… yes
checking expat.h presence… yes
checking for expat.h… yes

but there is still the undefined symbol error.

So I have to put any expat function between the brackets?
Like AST_EXT_LIB_CHECK([EXPAT], [expat], [XML_ParserCreate], [expat.h])?

Yes, you do. That’s how it determines if the library is usable or not.

Great, It works, thanks!