hello everyone

I write project, there is problem in linking of my code

Error	1	error LNK2005: _str_list already defined in file1.obj	string-test\file2.obj	string-test
Error	2	error LNK2005: _str_list already defined in file1.obj	string-test\str.obj	string-test

think this is my code, anybody have any idea

main.c

int main()
{
	print1();
	print2();

	return 0;
}

str.c

#include "str.h"

char *get_str(int i)
{
	return str_list[i];
}

str.h

#ifndef __HEADER_STR__
#define __HEADER_STR__

char *str_list[] = { "text1", "text2", "text3" };

char *get_str(int i);

#endif

file1.c

#include <stdio.h>
#include "str.h"

void print1(void)
{
	printf("%s\n", get_str(0));
}

file1.h

#ifndef __HEADER_1__
#define __HEADER_1__

void print1(void);

#endif

file2.c

#include <stdio.h>
#include "str.h"

void print2(void)
{
	printf("%s\n", get_str(1));
}

file2.h

#ifndef __HEADER_2__
#define __HEADER_2__

void print2(void);

#endif

thanks

Never define variables in a header file (.h)
They go into source files (.c)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.