delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2005/01/20/18:01:50

Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
From: svoboda AT cs DOT cmu DOT edu
Reply-To: svoboda AT cs DOT cmu DOT edu
To: cygwin AT sourceware DOT org
Subject: Can g++ DLLs and main program share variables?
Date: Thu, 20 Jan 2005 18:00:49 -0500
User-Agent: KMail/1.7.2
Cc: David Svoboda <svoboda AT cs DOT cmu DOT edu>
References: <200501171746 DOT 26708 DOT svoboda AT cs DOT cmu DOT edu> <41ECE515 DOT 8030905 AT free DOT fr>
In-Reply-To: <41ECE515.8030905@free.fr>
MIME-Version: 1.0
Message-Id: <200501201800.50303.svoboda@cs.cmu.edu>
Source-Info: Sender is really svoboda+@mandal.lti.cs.cmu.edu

Thanks for the help re building DLL's with g++. I have DLLs which I
can build and link with my main code. But the DLLs seem to have
separate variables...how do I load a DLL so that it uses the same
'namespace' as my main code?

Here is a simple example I've built up. The main function calls the
hello function inside a dll, and hello increments a static 'value'
variable from 3 to 4. But in the main function, the value variable,
also defined, still remains at 3.

How do I get the GOOD output from running './main.exe'?

Thanks!
~David Svoboda


==> foo.C <==

int value = 3;

void inc() {
  value++;
}


==> foo.h <==

extern int value;

void inc();

==> hello.C <==
#include <stdio.h>
#include "foo.h"

extern "C" {
  int hello(void*);
}


int hello(void* name) {
  printf("Hello, %s!\n", (char*) name);
  printf("In hello, Value is %d\n", value);
  inc();
  printf("In hello, Value is %d\n", value);
  return 0;
}


==> main.C <==
#include <stdio.h>
#include "foo.h"

extern "C" {
  int dlopen(char*);
  int dlsym( int, char*);
  int dlclose( int);
  char* dlerror();
}

typedef void (*func_t)(const void*);

int main() {
  int handle;
  int fn;
  if ((handle = dlopen("hello.dll")) == 0)
    printf( "dlopen: can't open libhello.dll\n");
  if ((fn = dlsym( handle, "hello")) == 0)
    printf("dlsym: can't open hello()\n");
  func_t f = (func_t) fn;

  printf("In main, Value is %d\n", value);
  f("Dave");
  printf("In main, Value is %d\n", value);
  inc();
  printf("In main, Value is %d\n", value);
  return 0;
}


==> Makefile <==

all:		main.exe hello.dll

clean:
	rm *# *~ *.o *.dll *.exe *.stackdump || true

main.exe:	main.o foo.o
	g++ -o main.exe main.o foo.o

hello.dll:	hello.o foo.o
	g++ -shared -o hello.dll hello.o foo.o

==> output_bad <==
In main, Value is 3
Hello, Dave!
In hello, Value is 3
In hello, Value is 4
In main, Value is 3
In main, Value is 4

==> output_good <==
In main, Value is 3
Hello, Dave!
In hello, Value is 3
In hello, Value is 4
In main, Value is 4
In main, Value is 5



--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019