-1

I am trying a code which has two C++ files(client.cpp and server.cpp) and one common.h header file which contais a class and functions. If client stops execution server should stop as well. For that purpose I thought of a global variable as suggested in :How do I use extern to share variables between source files? but it didn't work out for me.

Error : when I tried g++ server.cpp common.h -o server:

/tmp/ccjoNSQd.o: In function `main':
server.cpp:(.text+0x3a): undefined reference to `ch'
collect2: error: ld returned 1 exit status

client.cpp:

#include "common.h"

int ch=1;
int main()
{

  random r;
  do{
    cout<<"Do you want to continue: 1=yes 0=no "<<endl;
    cin>>ch;
    r.display();
    }while(ch==1);
}

server code:

#include "common.h"

int main()
{
    random r;
  do{
    cout<<"display in server"<<endl;
    r.display();
    }while(ch==1);
}

common.h:

#ifndef COMMON_H
#define COMMON_H
#include <iostream>
using namespace std;
extern int ch;
class random
{
    public:
        void display()
        {
          cout<<"HELLO_WORLD"<<endl;
        } 
 }; 


#endif

I want to include the common.h in both the files . So according to How do I use extern to share variables between source files? I've written extern int ch in common.h and used ch in both source files.

Community
  • 1
  • 1
  • You're missing something that's mentioned in the accepted answer to the question you linked to... Look carefully at all the references to `global_variable` in that answer. The author of it is doing something that you're not. – skrrgwasme Apr 09 '16 at 17:18
  • 1
    I don't think this will do what you want. You can set up the source files so that there's a variable with a common name in both the client and server programs, but setting the variable in one program won't change the value of the variable in the other program. – Graeme Perrow Apr 09 '16 at 17:19

3 Answers3

1

What you want will only work only in single compilation. To share variable between translation units you must move int ch=1; to global scope, outside any function. Then you can reference it from other translation unit if you declare it extern int ch;

To share some data between two different applications you must use some Inter Process Communication mechanism. The simplest one is some external file. This is a broad topic, you can find many answers on SO: Sharing same variable between more than one independent programs in Linux

For platform independent solution I suggest using boost::interprocess library: http://www.boost.org/doc/libs/1_60_0/doc/html/interprocess/sharedmemorybetweenprocesses.html

Community
  • 1
  • 1
marcinj
  • 48,511
  • 9
  • 79
  • 100
  • I did edit code as you suggested but still getting same error. Can you please explain correction using some example? –  Apr 09 '16 at 17:23
  • hmm, you have two .cpp files and each has main - why? You want to compile two separate executables and share variable between them? You cannot do that using extern. – marcinj Apr 09 '16 at 17:26
  • then can you suggest simplest solution to this with example? –  Apr 09 '16 at 17:28
  • No, there is no simple solution. If you have two programs you can't use a shared variable. You have to communicate between the two programs. And that is not trivial to do. You have to set up some communication process between the two. – rsjaffe Apr 09 '16 at 17:30
  • posix_message_queues? –  Apr 09 '16 at 17:32
  • 1
    @Deadpool look at the boost::interprocess library link in my answer, it provides some very simple examples. – marcinj Apr 09 '16 at 17:35
0

This isn't going to work the way you think. You are compiling like this: "g++ server.cpp common.h -o server". That tells me that you are probably also compiling the client like this (or something similar): "g++ client.cpp common.h -o client".

This is going to create two separate executables. If you were compiling them into one program, you could do as the other answers suggest here: move int ch; into the global scope (outside of any functions, including main) of exactly one of the files, and ch would be shared between both files.

But you're not doing that - you're creating two entirely separate programs. You can't have a shared global variable between two programs. You need to use some other method of communicating such as a sockets or TCP. If they're running on the same system, you could also use a simple hack like having one process write to a text file that the other reads.

skrrgwasme
  • 9,358
  • 11
  • 54
  • 84
-1

in one and only one C file write (without extern) at main level (not in function)

int ch;

BUT not declare in main, beause the first will be hidden, You can set value

ch = 1;

but not declare;

Jacek Cz
  • 1,872
  • 1
  • 15
  • 22