Hi,

The problem has been solved. I report it back here in case it might be helpful to others who would run into similar problems.
Basically I added switch_core_init(..) and set parameters for the global variable: SWITCH_GLOBAL_dirs

Below are the working codes:

======================================================================
#include <iostream>
#include <switch.h>
#include <QDir>

int main(int argc, char** argv)
{
    switch_core_flag_t flags = SCF_USE_SQL;
    bool console = true;
    const char *err = NULL;

    switch_core_set_globals();

    QDir conf_dir = QDir::current();

    SWITCH_GLOBAL_dirs.conf_dir = (char *) malloc(strlen(QString("%1/conf").arg(conf_dir.absolutePath()).toAscii().constData()) + 1);
    if (!SWITCH_GLOBAL_dirs.conf_dir) {
       fprintf(stderr, "Cannot allocate memory for conf_dir.");
    }
    strcpy(SWITCH_GLOBAL_dirs.conf_dir, QString("%1/conf").arg(conf_dir.absolutePath()).toAscii().constData());

    fprintf(stderr, "AAA%s\n",SWITCH_GLOBAL_dirs.conf_dir);

    SWITCH_GLOBAL_dirs.log_dir = (char *) malloc(strlen(QString("%1/log").arg(conf_dir.absolutePath()).toAscii().constData()) + 1);
    if (!SWITCH_GLOBAL_dirs.log_dir) {
           fprintf(stderr,"Cannot allocate memory for log_dir.");
    }
    strcpy(SWITCH_GLOBAL_dirs.log_dir, QString("%1/log").arg(conf_dir.absolutePath()).toAscii().constData());

    SWITCH_GLOBAL_dirs.run_dir = (char *) malloc(strlen(QString("%1/run").arg(conf_dir.absolutePath()).toAscii().constData()) + 1);
    if (!SWITCH_GLOBAL_dirs.run_dir) {
        fprintf(stderr,"Cannot allocate memory for run_dir.");
    }
    strcpy(SWITCH_GLOBAL_dirs.run_dir, QString("%1/run").arg(conf_dir.absolutePath()).toAscii().constData());

    SWITCH_GLOBAL_dirs.db_dir = (char *) malloc(strlen(QString("%1/db").arg(conf_dir.absolutePath()).toAscii().constData()) + 1);
    if (!SWITCH_GLOBAL_dirs.db_dir) {
        fprintf(stderr,"Cannot allocate memory for db_dir.");
    }
    strcpy(SWITCH_GLOBAL_dirs.db_dir, QString("%1/db").arg(conf_dir.absolutePath()).toAscii().constData());

    SWITCH_GLOBAL_dirs.script_dir = (char *) malloc(strlen(QString("%1/script").arg(conf_dir.absolutePath()).toAscii().constData()) + 1);
    if (!SWITCH_GLOBAL_dirs.script_dir) {
        fprintf(stderr,"Cannot allocate memory for script_dir.");
    }
    strcpy(SWITCH_GLOBAL_dirs.script_dir, QString("%1/script").arg(conf_dir.absolutePath()).toAscii().constData());

    SWITCH_GLOBAL_dirs.htdocs_dir = (char *) malloc(strlen(QString("%1/htdocs").arg(conf_dir.absolutePath()).toAscii().constData()) + 1);
    if (!SWITCH_GLOBAL_dirs.htdocs_dir) {
        fprintf(stderr,"Cannot allocate memory for htdocs_dir.");
    }
    strcpy(SWITCH_GLOBAL_dirs.htdocs_dir, QString("%1/htdocs").arg(conf_dir.absolutePath()).toAscii().constData());

    SWITCH_GLOBAL_dirs.grammar_dir = (char *) malloc(strlen(QString("%1/grammar").arg(conf_dir.absolutePath()).toAscii().constData()) + 1);
    if (!SWITCH_GLOBAL_dirs.grammar_dir) {
        fprintf(stderr,"Cannot allocate memory for grammar_dir.");
    }
    strcpy(SWITCH_GLOBAL_dirs.grammar_dir, QString("%1/grammar").arg(conf_dir.absolutePath()).toAscii().constData());

    /* Initialize the core and load modules, that will startup FS completely */
    //if (switch_core_init(flags, console, &err) != SWITCH_STATUS_SUCCESS) {
    if (switch_core_init(flags, console ? SWITCH_TRUE : SWITCH_FALSE, &err) != SWITCH_STATUS_SUCCESS) {
        fprintf(stderr, "Failed to initialize FreeSWITCH's core: %s\n", err);
    }

    //switch_core_init_and_modload(flags, console ? SWITCH_TRUE : SWITCH_FALSE, &err);
    switch_core_init_and_modload(flags, SWITCH_FALSE, &err);
    switch_core_runtime_loop(!console);
    return 0;

}

====================================================================

cheers,
Xing



On 22/11/11 16:47, Anthony Minessale wrote:
maybe your user does not have permissions to read and write the target install dir 
try to chown -R to your user on all of /usr/local/freeswitch

you might want to try things the other way around and embedd your app in FS rather than embed FS in your app.


On Tue, Nov 22, 2011 at 10:20 AM, xl127 <x.liu@hw.ac.uk> wrote:
If I switch to root user I can successfully run my embedded FS app: FS_Embed.
but it will fails if I run it as non-root user.

One thing I can think of to do is re-install FS as non-root.
I tried but it failed to access /usr/lib/python2.7/site-packages/freeswitch.py due to the permissions.



On 22/11/11 16:08, Anthony Minessale wrote:
i know you needed apr, not sure, maybe try strace or gdb


On Tue, Nov 22, 2011 at 6:36 AM, xl127 <x.liu@hw.ac.uk> wrote:
Thanks for the advice!

I tried it but still got the same error.

I added the #include <apr_general.h> and found it from /usr/local/sr/freeswitch/libs/apr/include
rather than from the installed directory /usr/local/freeswitch. Hope it is the right one.

any more clues?



On 22/11/11 01:38, Anthony Minessale wrote:
try 

    if (apr_initialize() != SWITCH_STATUS_SUCCESS) {
        fprintf(stderr, "FATAL ERROR! Could not initialize APR\n");
        return 255;
    }

early on in main()


On Mon, Nov 21, 2011 at 1:12 PM, xl127 <x.liu@hw.ac.uk> wrote:
Hello,

Following the instructions in
http://wiki.freeswitch.org/wiki/Embedding_FreeSWITCH
I am trying to use Embedded FreeSwitch.

I installed FS in Fedora linux as a root user in /usr/local/freeswitch.
Now as non-root user,
I compiled following codes:

#include <switch.h>
int main(int argc, char** argv)
{
    switch_core_flag_t flags = SCF_USE_SQL;
    bool console = true;
    const char *err = NULL;
    switch_core_set_globals();
    switch_core_init_and_modload(flags, console ? SWITCH_TRUE :
SWITCH_FALSE, &err);
    switch_core_runtime_loop(!console);
    return 0;
}

And copy the executable and conf, mod, contents of lib of the installed
FS to my $MyWorkingDir.

My directory structure looks like:
   MyWorkingDir--bin/  (contains my executable: FS_Embed)
                        --conf/
                        --mod/
                        --lib/
                        --grammars
                        --sounds
                        --scripts
                        --log
(I also tried to copy the contents of lib directory into MyWorkingDir)

When I run my embedded FS: ./FS_Embed, I got error

2011-11-21 18:46:26.606783 [INFO] switch_event.c:631 Activate Eventing
Engine.
2011-11-21 18:46:26.617941 [DEBUG] switch_event.c:610 Create event
dispatch thread 0
FS_Embed: src/switch_xml.c:2225: switch_xml_open_cfg: Assertion
`MAIN_XML_ROOT != ((void *)0)' failed.
Aborted (core dumped)

and occasionally on another run, I got error:

2011-11-21 16:56:51.756040 [INFO] switch_event.c:631 Activate Eventing
Engine.
2011-11-21 16:56:51.767198 [DEBUG] switch_event.c:610 Create event
dispatch thread 0
Segmentation fault (core dumped)

It looks like there are some things wrong with my Environment Setup for
Embedded FS,
but after a while of checking/trying/googling I couldn't find what's wrong.

Any advice please?

Many thanks!
Xing



--
Heriot-Watt University is a Scottish charity
registered under charity number SC000278.

Heriot-Watt University is the Sunday Times
Scottish University of the Year 2011-2012



_________________________________________________________________________
Professional FreeSWITCH Consulting Services:
consulting@freeswitch.org
http://www.freeswitchsolutions.com

FreeSWITCH-powered IP PBX: The CudaTel Communication Server
http://www.cudatel.com

Official FreeSWITCH Sites
http://www.freeswitch.org
http://wiki.freeswitch.org
http://www.cluecon.com

FreeSWITCH-users mailing list
FreeSWITCH-users@lists.freeswitch.org
http://lists.freeswitch.org/mailman/listinfo/freeswitch-users
UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users
http://www.freeswitch.org



--
Anthony Minessale II

FreeSWITCH http://www.freeswitch.org/
ClueCon http://www.cluecon.com/
Twitter: http://twitter.com/FreeSWITCH_wire

AIM: anthm
MSN:anthony_minessale@hotmail.com
GTALK/JABBER/PAYPAL:anthony.minessale@gmail.com
IRC: irc.freenode.net #freeswitch

FreeSWITCH Developer Conference
sip:888@conference.freeswitch.org
googletalk:conf+888@conference.freeswitch.org
pstn:+19193869900


_________________________________________________________________________
Professional FreeSWITCH Consulting Services:
consulting@freeswitch.org
http://www.freeswitchsolutions.com

FreeSWITCH-powered IP PBX: The CudaTel Communication Server
http://www.cudatel.com

Official FreeSWITCH Sites
http://www.freeswitch.org
http://wiki.freeswitch.org
http://www.cluecon.com

FreeSWITCH-users mailing list
FreeSWITCH-users@lists.freeswitch.org
http://lists.freeswitch.org/mailman/listinfo/freeswitch-users
UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users
http://www.freeswitch.org




MailScanner Signature HW   Heriot-Watt University is the Sunday Times

  Scottish University of the Year 2011-2012


  Heriot-Watt University is a Scottish charity
  registered under charity number SC000278.

_________________________________________________________________________
Professional FreeSWITCH Consulting Services:
consulting@freeswitch.org
http://www.freeswitchsolutions.com

FreeSWITCH-powered IP PBX: The CudaTel Communication Server
http://www.cudatel.com

Official FreeSWITCH Sites
http://www.freeswitch.org
http://wiki.freeswitch.org
http://www.cluecon.com

FreeSWITCH-users mailing list
FreeSWITCH-users@lists.freeswitch.org
http://lists.freeswitch.org/mailman/listinfo/freeswitch-users
UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users
http://www.freeswitch.org




--
Anthony Minessale II

FreeSWITCH http://www.freeswitch.org/
ClueCon http://www.cluecon.com/
Twitter: http://twitter.com/FreeSWITCH_wire

AIM: anthm
MSN:anthony_minessale@hotmail.com
GTALK/JABBER/PAYPAL:anthony.minessale@gmail.com
IRC: irc.freenode.net #freeswitch

FreeSWITCH Developer Conference
sip:888@conference.freeswitch.org
googletalk:conf+888@conference.freeswitch.org
pstn:+19193869900


_________________________________________________________________________
Professional FreeSWITCH Consulting Services:
consulting@freeswitch.org
http://www.freeswitchsolutions.com

FreeSWITCH-powered IP PBX: The CudaTel Communication Server
http://www.cudatel.com

Official FreeSWITCH Sites
http://www.freeswitch.org
http://wiki.freeswitch.org
http://www.cluecon.com

FreeSWITCH-users mailing list
FreeSWITCH-users@lists.freeswitch.org
http://lists.freeswitch.org/mailman/listinfo/freeswitch-users
UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users
http://www.freeswitch.org




MailScanner Signature HW   Heriot-Watt University is the Sunday Times
  Scottish University of the Year 2011-2012


  Heriot-Watt University is a Scottish charity
  registered under charity number SC000278.

_________________________________________________________________________
Professional FreeSWITCH Consulting Services:
consulting@freeswitch.org
http://www.freeswitchsolutions.com

FreeSWITCH-powered IP PBX: The CudaTel Communication Server
http://www.cudatel.com

Official FreeSWITCH Sites
http://www.freeswitch.org
http://wiki.freeswitch.org
http://www.cluecon.com

FreeSWITCH-users mailing list
FreeSWITCH-users@lists.freeswitch.org
http://lists.freeswitch.org/mailman/listinfo/freeswitch-users
UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users
http://www.freeswitch.org




--
Anthony Minessale II

FreeSWITCH http://www.freeswitch.org/
ClueCon http://www.cluecon.com/
Twitter: http://twitter.com/FreeSWITCH_wire

AIM: anthm
MSN:anthony_minessale@hotmail.com
GTALK/JABBER/PAYPAL:anthony.minessale@gmail.com
IRC: irc.freenode.net #freeswitch

FreeSWITCH Developer Conference
sip:888@conference.freeswitch.org
googletalk:conf+888@conference.freeswitch.org
pstn:+19193869900


_________________________________________________________________________
Professional FreeSWITCH Consulting Services:
consulting@freeswitch.org
http://www.freeswitchsolutions.com

FreeSWITCH-powered IP PBX: The CudaTel Communication Server
http://www.cudatel.com

Official FreeSWITCH Sites
http://www.freeswitch.org
http://wiki.freeswitch.org
http://www.cluecon.com

FreeSWITCH-users mailing list
FreeSWITCH-users@lists.freeswitch.org
http://lists.freeswitch.org/mailman/listinfo/freeswitch-users
UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users
http://www.freeswitch.org




MailScanner Signature HW   Heriot-Watt University is the Sunday Times
  Scottish University of the Year 2011-2012


  Heriot-Watt University is a Scottish charity
  registered under charity number SC000278.