473,624 Members | 2,234 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sharing a variable between two c files

48 New Member
I am trying to share a variable between two c files

common.h

Expand|Select|Wrap|Line Numbers
  1. extern char seq2[1000000];
  2.  
File1.c

Expand|Select|Wrap|Line Numbers
  1. #include "common.h"
  2.  
  3. char seq2[1000000] = {0};
  4. void main(){}
  5.  
File2.c

Expand|Select|Wrap|Line Numbers
  1. #include "common.h"
  2.  
  3. int main(){
  4.  
  5.     if(seq2[0] != '.'){
  6.       int j = 1;
  7.       val = seq2[j-1];
  8.       seq2[j-1] = '.';
  9.     }
  10. }
  11.  

and i am compiling the files like

gcc File1.c -o f1
gcc File2.c -o f2


but i am getting error undefined reference to `seq2'.Can someone explain my mistake i tried multiple method on internet but none works.
Jun 5 '17 #1
1 1098
weaknessforcats
9,208 Recognized Expert Moderator Expert
seq2 is an array.

This code defines the array:

Expand|Select|Wrap|Line Numbers
  1. char*seq2[1000000]*=*{0};
  2.  
so every time you include this line you get a new array. The compile dies with multiple definitions.

What you do is put this line of code in a .c file so the array is defined only once. Use the extern.

Next, since this is an array, the name of the array (seq2) is the address of seq2[0]. Since seq2 is an array of char, the name seq2 is the address of a char. Put this in your common .h:

Expand|Select|Wrap|Line Numbers
  1. extern char* seq2;
Notice you have lost the size of the array in common.h. This is called decay of array. More info here about arrays: https://bytes.com/topic/c/insights/7...rrays-revealed

I suggest you add an int variable to the file that defines the array and initialize it to the sizeof the array. Then add this variable to your common.h so other files can see the address of the array and the size.

BTW: This seq is an array of a million chars. It is defined as a local stack variable. Some C compilers limit the size of the stack to as little as 4096 bytes. I suggest you allocate the array dynamically using malloc() so all that's on the stack is the address of the allocation.

You would need to allocate the array in main() before using it.
Jun 5 '17 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
6536
by: Dave L | last post by:
I have a Managed C++ DLL used by a C# application. I'm building with VS .NET 2002. I have a resx file loaded with string data. The C# application can access the string data fine, like so: ResourceManager rm = new ResourceManager("DS.Names", Assembly.GetExecutingAssembly()); String text = rm.GetString("1");
5
1776
by: Manuel Arroba | last post by:
I had the idea that two users code are being run by two different instancies, so from the variable values point of view they are complete independent. I have an asp.net that calls a module rutine. When there are two users accessing to the module rutine, looks like asp.net is sharing variable values. For example, in one point I check the size of the table (from debug for example I now is 3) but is reporting an incorrect size (let says...
1
1198
by: groston | last post by:
I have a number of applications that run on the same sever (and my development machine). Is there an easy way to share the same style sheet between these apps? I assume that I can do a <LINK href="http://<full path/Styles.css" type="text/css" rel="stylesheet"> but then the web pages for my development machine and the server would be different.
0
1275
by: John Lafrowda | last post by:
Dear all, here is a simple problem that I cannot overcome: I'm trying to write a client/server application in Visual Basic .net. The server is an executable (.exe) project, the clients are class library (.dll) projects. The server loads the clients from their DLL-files at runtime using reflection tools, such as: ClientAssembly = System.Reflection.Assembly.LoadFrom(Client_To_Load_File_Name)
2
2872
by: gavin | last post by:
I have two forms and wish to share some information between them. I declare the array as Shared in the fisrt class but cannot reference it in the second class. code is:- Public Class DataEntry1 Inherits System.Windows.Forms.Form Shared deArray(10) As String #Region " Windows Form Designer generated code " …
3
2502
by: M Schnell | last post by:
All, I've run into a problem and I'm hoping someone here can help me. I've got a ASP.NET solution with six projects within. The main web project has several resource files (.resx) to support foreign languages. However, I need to be able to pull translated text messages out of the resource files from a supporting project. The problem is I can't reference the main web app from the supporting project because that would set up a circular...
2
1293
by: Andrew E Chapman | last post by:
I have a VB.NET class library project and two other VB.NET projects that depend on that class library, ie: Class library --Project 1 --Project 2 Currently, as you'd expect, when I compile this, each of Project 1 and 2 end up with an exe and a dll. However, I don't want the project to be distributed with a dll, so is it possible to add the files to each of the Projects
1
1970
by: bruce.1 | last post by:
I'm trying to figure a way for a remote user -- using ASP.NET -- to share files on a local office LAN. The remote user would go to an ASP.NET website (on the LAN), mark the files they want, and ASP.NET would facilitate a download, and keep a record in a small database of who downloaded and uploaded whatever. These are big files, say 4 MB, so it's best if the remote user downloads the file, edits it, then uploads it back to some folder...
2
2275
by: David | last post by:
How do I share code across files in C#, without including a reference to a class library. Here's what I want to do. I have a device I want to talk to. It's a machine in a factory. It can talk on a serial port, or over ethernet. Either way it's talking, it sends the same data. So, I encapsulate the data structure in a class, and I stick the class in a .cs file. Now, I want to create three projects, and they all will deal with the...
21
34381
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most obvious of which is the sharing of files. For example, you upload images to a server to share them with other people over the Internet. Perl comes ready equipped for uploading files via the CGI.pm module, which has long been a core module and allows users...
0
8242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8681
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8341
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8488
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7170
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4183
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1793
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.