LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-03-2005, 06:04 PM   #1
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Rep: Reputation: 45
declare and initialize variables in a header file


I have the following two files
1.c
1.h
The 1.c includes the 1.h

In the 1.h i have tried to declare and initialize some variables
oid IpInReceives[]={ 1, 3, 6, 1, 2, 1, 4, 3};

When i have tried to compile it
I get this message
/tmp/cc9OXXPK.o(.data+0x0): In function `snmp_fetch_oid_value':
/home/alaios/code/monitor.c:23: multiple definition of `myoida'
/tmp/ccW5SXVh.o(.data+0x0):/home/alaios/code/3threads.c:10: first defined here
collect2: ld returned 1 exit status

I have searched using the grep and nowhere else this variable is initializes.. I have also noticed that when i removed tha declaration and the initialization from the header file to the .c file.. Everything gone smoothly...
Why?
 
Old 09-03-2005, 06:26 PM   #2
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
If you want an array to be global and if you want to be able to change the contents of the elements, declare it extern in the header. Then define it in a source file. Like this:

Code:
extern int array[]; /* in globals.h" */
Code:
#include "globals.h"
int array[] = {1, 2, 3}; /* in globals.c */
If the global array is supposed to be non-changing, you don't need to declare it extern, just declare static const in the header file and skip the definition in the c-file.
Code:
static const int an_array_that_never_changes_the_values_of_its_elements[] = {1, 2, 3}; /* in globals.h */
 
Old 09-03-2005, 07:59 PM   #3
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
Thx i have tried the static const but it returns me now the compiler thw following warning
warning: passing arg 2 of `snmp_fetch_oid_value' discards qualifiers from pointer target type
 
Old 09-04-2005, 04:25 AM   #4
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
You're not supplying very much information. Show me exactly how the variable is the declared, show me the function and show me how you pass the variable. I'm sure it can be condensed into a small test program that you can post here (inside [ code ] [ /code ] tags).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
header file problems in g++ crash_override_me Programming 5 10-07-2005 01:28 AM
Problem with c header file alaios Programming 4 09-03-2005 09:06 PM
Create header file for existing .c file hk_michael Programming 5 02-25-2005 05:26 PM
Using same header file in multiple sources george_mercury Programming 2 02-25-2005 01:50 PM
header file location ..mach.h .. pashupathinath Programming 1 10-21-2004 06:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration