gcc vs g++ for dlopen

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi all

Im writed small test program for dlopen
int main (int argc, char **argv)
{
	int   exitcode    = EXIT_OK;
	char *dlopen_name = NULL;
	char *dlsym_file  = NULL;
	void *dll;
	int   i;

	for (i=1; i<argc; ++i)
	{
		if (0==strcmp(argv[i], "-dlopen"))
		{
			dlopen_name = argv[++i];
		}
		else if (0==strcmp(argv[i], "-dlsym"))
		{
			dlsym_file = argv[++i];
		}
		else
		{
			printf ("%s", USAGE);
			return EXIT_EUSAGE;
		}
	}

	printf ("\n");

	dll = dlopen (dlopen_name, /*RTLD_NOW*/RTLD_LAZY);
	if (!dll)
	{
		fprintf (stderr, "ERR dlopen(%s) %s\n", dlopen_name, dlerror());
		return EXIT_EDLOPEN;
	}
	printf ("OK dlopen(%s)=%p\n", dlopen_name, dll);


	if (dlsym_file)
	{
		char line[1024]={0};

		FILE *in = fopen (dlsym_file, "r");
		if (!in)
		{
			fprintf (stderr, "ERR fopen(%s) %s\n", dlsym_file, strerror(errno));
			return EXIT_EREAD;
		}

		while (fgets(line, sizeof(line)/sizeof(line[0])-1, in))
		{
			void *addr;
			char *eol = strchr(line, '\n');
			if (eol) *eol = '\0';
			if (strchr(line, '#'))
				continue;

			addr = dlsym(dll, line);
			if (!addr)
			{
				fprintf (stderr, "ERR dlsym(%s) %s\n", line, dlerror());
				exitcode = EXIT_EDLSYM;
			}
			else
			{
				printf ("OK dlsym(%s)=%p\n", line, addr);
			}
		}
		exitcode = exitcode!=EXIT_OK? exitcode : ferror(in)? EXIT_EREAD : EXIT_OK;
		fclose(in);
	}

	return exitcode;
}

when compiled this test program with:
gcc -c test_library.c -o test_library1.o
g++ test_library1.o -o test_library1 -ldl

#>test_library1 -dlopen ./libasim.so
OK dlopen(./libasim.so)=0x804b020

but, when i build with:
gcc -ldl test_library.c -o test_library

#>test_library -dlopen ./libasim.so
Segmentation fault


I need gcc (second) option.

help me, please.

BR

-- 
View this message in context: http://www.nabble.com/gcc-vs-g%2B%2B-for-dlopen-tp20699786p20699786.html
Sent from the gcc - Help mailing list archive at Nabble.com.


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux