0

I have a c++ library linked to main.c. There is a global variables used in between the two files. The variables are declared in sharedata.h

#ifndef __SHARE_DATA_H__
#define __SHARE_DATA_H__
#include <stdio.h>
#include <pthread.h>

#ifdef __cplusplus
extern "C" {
#endif

// declare your two vars in the header file as extern. 
extern pthread_mutex_t mutex;
extern int *ptr;


#ifdef __cplusplus
}
#endif

#endif /* __SHARE_DATA_H__ */

Inside main.c, I have declaration and memallocation of ptr. And I can read and print of data in *ptr.

main.c
#include "sharedata.h"
pthread_mutex_t mutex;
int *ptr;
int main(){
  pthread_mutex_lock( &mutex );
  ptr = (int *)malloc((4 * 5) * sizeof(int)); 
  pthread_mutex_unlock( &mutex );
  /*do some other things
     %%%%%%%%%%%%%%%%%
     %%%%%%%%%%%%%%%%%
  */
  pthread_mutex_lock( &mutex ); 
  for (int x = 0; x < 5; x++)
  {     
    printf("%d ", *(ptr+x));         
  }
  printf("\n");
  pthread_mutex_unlock( &mutex ); 
}

Inside gstdsexample.cpp, I have declaration and initialization of global variables. But when I try to write data to *ptr, I have segmentation fault at this line *(ptr+x) = 1; What could be wrong?

#include "sharedata.h"
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int *ptr;

{
   printf("before lock\n");
   pthread_mutex_lock( &mutex ); 
   for (int x = 0; x < 5; x++)
   {     
      printf("before pointer\n");
      *(ptr+x) = 1;      
   }
   pthread_mutex_unlock( &mutex ); 

}
batuman
  • 7,066
  • 26
  • 107
  • 229
  • 2
    You have defined the variables in two source files - a One Definition Rule (ODR) violation. Pick one, remove the definitions from the other. – Igor Tandetnik Nov 25 '21 at 03:42
  • If I don't define in main.c, I have undefined reference error in compilation. – batuman Nov 25 '21 at 03:43
  • 2
    Are you even linking in `gstdsexample.cpp`? It doesn't look right - there's code there outside of any function. It shouldn't compile. Most likely, you are only compiling `main.c`, and there it fails because `mutex` is not initialized; in C, you are supposed to call `pthread_mutex_init` – Igor Tandetnik Nov 25 '21 at 03:44
  • Yes I linked gstdsexample.so to main.c. This is just a small part of the whole code. – batuman Nov 25 '21 at 03:46
  • When you use two underscores in a row in a C++ identifier, [things can get weird](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). It doesn't happen very often, but when it does, the errors are nigh inscrutable, so it's better to be not risked at all. – user4581301 Nov 25 '21 at 03:46
  • If I take out variable defination from gstdsexample.cpp file or mani.c, compilation gives `undefined reference to `mutex' and undefined reference to `ptr'` – batuman Nov 25 '21 at 03:47
  • You meant this `__cplusplus` – batuman Nov 25 '21 at 03:49
  • Prepare a [mcve]. The problem is likely somewhere in the code not shown, and/or in the build setup. Definitions should be in exactly one source file. – Igor Tandetnik Nov 25 '21 at 03:49
  • Bulid set up was working fine. That is existing project working fine. I just add those variables as I need additional work on the project. I tried best to show the issue. – batuman Nov 25 '21 at 03:51
  • I understand what you meant now. The library is run time library. So compilation time, I have error as undefined reference. What should be solution to this? – batuman Nov 25 '21 at 04:06
  • where is sharedata.h ? Are you sure the included in main.c is correct one you edited? – Vagish Nov 25 '21 at 04:39
  • @Vagish yes i did – batuman Nov 25 '21 at 04:43

1 Answers1

2

First of all, sharing global files across several files is terrible practice. In this case you should probably create wrapper functions handling all access to this variable, including the mutex. Declare those setter/getter function in someheader.h and then define the variables in someheader.c only. And make them static.

In the case where you do use global variables (which you shouldn't do), then there should be one single extern reference in a header file and one single variable definition in a .c file. You have multiple variable definitions so neither the variable nor the mutex will work.

Lundin
  • 195,001
  • 40
  • 254
  • 396