Difficulty in compiling a C programme on a C++ compiler

Hello, can someone point out where I am going wrong please? My understanding of C is a bit rusty and C++ is a non-starter (did Visual C++ in an evening class years ago and can't remember any of it). I have written a complicated programme in C on my old PC (Windows XP) using the compiler 'Code::Blocks'. It works fine, it re-arranges numbers on an array in a recursive manner and prints to file and to the (I think it's called) the DOS Command Shell. However, I have just got myself a new machine running Windows 7, onto which I have downloaded the Microsoft Visual C++ 2008 Express Edition compiler, it gives me plenty of errors and am out of my depth, both with the programming and Windows 7. I used this compiler because I could not find a free C compiler for a 64 bit machine and a friend said that I can run C on a C++ compiler, suggesting I try this one.
I created a project and opened blank files into which I pasted the code for a few functions, here is an example of the code:


In the header file, structures.h
#define NUMBER 100
char direction = 'x';
char filename[30];
int level = 0;
struct object_t
{
int ident;
int in_use;
};


in the main file:
#include <stdio.h>
#include <string.h>
#include "structures.h"
struct object_t object[NUMBER];
extern struct object_t object[NUMBER]; /* not sure why I have this line, but the original preferred it this way */
extern void Initialise();
extern int level;
extern char filename[];
extern char direction;
void main(void)
{
direction = 'n';
Initialise();
}



And the Initialise function:
#include <stdio.h>
#include "structures.h"
extern struct object_t object[NUMBER];
void Initialise(void)
{
object[0].ident = 0; /* this is done so that objects can be moved, yet retain their identity */
object[1].ident = 1;

object[99].ident = 99;
}


There is a lot more, but this is heavily simplified.
I get a large number of error messages, most of them like this:

1>Initialise.obj : error LNK2005: "char * filename" (?filename@@3PADA) already defined in Display_board.obj

Which is strange, because the filename is not used in the Display_board function. The other messages are similar, except for different function and variable names.

My only book here is "The C Programming Language" by Kernighan & Ritchie", which I don't find a good source of reference. I found on this forum a question on external variables and tried what was said, to no avail. I also tried the compilers own help pages, but cannot see anything like what I am trying to do. I even tried experimenting with the code, but the result remained unchanged.

I would really appreciate it if someone can point out where I have gone wrong. But please note that being moderately dyslexic, I find it difficult to follow unclear instructions, so clear is good :-).
I can pick up any responses after the Easter break, sorry if this is long but I wanted it to be as clear as possible. Thank you.
I think the problem is simply that you do not have an include guard on your header file. What does that mean? It prevents code in a header file from being included more than once. Disch wrote a great article on this: http://www.cplusplus.com/forum/articles/10627/

Moral of the story is that you only have to do the following:
In structures.h...

1
2
3
4
5
6
#ifndef _STRUCTURES_H
#define _STRUCTURES_H

// ... header code goes in here

#endif _STRUCTURES_H 


So, if I'm not mistaken, the linker errors come up because everything in structures.h was being included twice (once in your main file, and again in the file that defines Initialise). Thus it complains that 'filename' is already defined etc.

Hope that helps. Happy Easter :).
Hello Sammy.
Thanks for that info, I shall give it a try when I pop home today and let you know how it goes.
Meanwhile, anticipating other problems, I have a few more questions.
1) Should I be able to compile a C programme on a C++ compiler without any changes or do I have a major conversion job ahead and if so what are the top issues? (my program has 1 header file, a main and abut 12 functions in about 4 nested layers. There's no maths, just equivalence testing, loops with if-else, do-while, for and switch; also the movement of numbers between arrays)

2) I did pick up from another thread about external declarations of what is defined in one place and used elsewhere, but I may have missed something. Where I have:
struct object_t object[NUMBER];
extern struct object_t object[NUMBER];

should it be like this?

3) Another error I had regarded declaration of a function call with parameters, it said something about no parameters (sorry, cant be specific, not in front of my computer)
Where i declare it, it looks like this:
extern void function_x();

when the prototype (is that the right word?) looks like this:
void function_x(int x, int y, char z)
what should the external declaration look like?

Thanks again for the input and a Happy Easter to you too. Remember, chocolate always wins :-)
Hey AyJay,

I'll attempt to answer your questions, but I'll say up front that I'm not 100% sure. I hope that one of the more experienced posters can confirm my information...

1) I don't think you will have too many problems compiling your C program on a C++ compiler. There are apparently some minor differences, perhaps check out http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html

2) If you're already including "structures.h", and object_t is defined there, then the definition will be available during the compilation and you only need to write:

object_t object[NUMBER];

3) I believe the external declaration should look the same:

extern void function_x(int x, int y, char z);

I hope that helps :).
Topic archived. No new replies allowed.