1 /*********************************************************************
2  * test-dynload.c
3  * test the ability to dlopen the gnc_module library and initialize
4  * it via dlsym
5  *********************************************************************/
6 /********************************************************************\
7  * This program is free software; you can redistribute it and/or    *
8  * modify it under the terms of the GNU General Public License as   *
9  * published by the Free Software Foundation; either version 2 of   *
10  * the License, or (at your option) any later version.              *
11  *                                                                  *
12  * This program is distributed in the hope that it will be useful,  *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
15  * GNU General Public License for more details.                     *
16  *                                                                  *
17  * You should have received a copy of the GNU General Public License*
18  * along with this program; if not, contact:                        *
19  *                                                                  *
20  * Free Software Foundation           Voice:  +1-617-542-5942       *
21  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
22  * Boston, MA  02110-1301,  USA       gnu@gnu.org                   *
23  *                                                                  *
24 \********************************************************************/
25 
26 
27 #include <config.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <gmodule.h>
31 #include <unittest-support.h>
32 
33 #include "gnc-module.h"
34 
35 int
main(int argc,char ** argv)36 main(int argc, char ** argv)
37 {
38     GModule *gmodule;
39     gchar *modpath;
40     const char *libdir = g_getenv("LIBDIR");
41 
42     if (libdir == NULL)
43     {
44         libdir = "../.libs";
45     }
46 
47     g_test_message("  test-dynload.c: testing dynamic linking of libgnc-module ...");
48 #ifdef G_OS_WIN32
49 /* MinGW builds libgnc-module-0.dll */
50     if (libdir == NULL)
51     {
52         modpath = g_module_build_path ("../.libs", "gnc-module-0");
53     }
54     else
55     {
56         modpath = g_module_build_path (libdir, "gnc-module");
57     }
58 #elif defined(GNC_PLATFORM_OSX)
59 /* We build libgnc-module as a shared library for testing, and on OSX
60  * that means that g_module_build_path (), which uses ".so", doesn't
61  * build the right path name.
62  */
63     if (libdir == NULL)
64     {
65         modpath = g_build_filename ("..", ".libs", "libgnc-module.dylib", NULL);
66     }
67     else
68     {
69         modpath = g_build_filename (libdir, "libgnc-module.dylib", NULL);
70     }
71 #else /* Regular Unix */
72     modpath = g_module_build_path (libdir, "gnc-module");
73 #endif
74     gmodule = g_module_open(modpath, 0);
75 
76     if (gmodule)
77     {
78         gpointer ptr;
79         if (g_module_symbol(gmodule, "gnc_module_system_init", &ptr))
80         {
81             void (* fn)(void) = ptr;
82             fn();
83             printf(" OK\n");
84             exit(0);
85         }
86         else
87         {
88             printf(" failed to find gnc_module_system_init\n");
89             exit(-1);
90         }
91     }
92     else
93     {
94         printf(" failed to open library.\n");
95         printf("%s\n", g_module_error());
96         exit(-1);
97     }
98 }
99