C: duplicate symbols for architecture x86_64
I am trying to create a struct in a header file, and initialize a template struct. For some reason, when including the header file in multiple files, it gives me the following error:
gcc foo.c bar.c -o foo -Wall
duplicate symbol _MYFOO in:
/var/folders/s4/zyw5lgk92wj9ljnsypgwdccr0000gn/T/foo-52f8fc.o
/var/folders/s4/zyw5lgk92wj9ljnsypgwdccr0000gn/T/bar-6dc21f.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
These are my files:
Bar.c
:
#include "bar.h"
#include <stdio.h>
void helloWorld() {
printf("Hello worldn");
}
Bar.h
typedef struct Foo Foo;
struct Foo {
int number;
} MYFOO = {2};
void helloWorld(void);
Foo.c
#include "bar.h"
int main() {
helloWorld();
}
Interestingly enough, when I remove the line containing
MYFOO = {2};
The code compiles and works perfectly fine. I believe it has to do with including Bar.h twice, which ends up including that struct twice? But how would I avoid something like that?
Thank you!
c gcc
add a comment |
I am trying to create a struct in a header file, and initialize a template struct. For some reason, when including the header file in multiple files, it gives me the following error:
gcc foo.c bar.c -o foo -Wall
duplicate symbol _MYFOO in:
/var/folders/s4/zyw5lgk92wj9ljnsypgwdccr0000gn/T/foo-52f8fc.o
/var/folders/s4/zyw5lgk92wj9ljnsypgwdccr0000gn/T/bar-6dc21f.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
These are my files:
Bar.c
:
#include "bar.h"
#include <stdio.h>
void helloWorld() {
printf("Hello worldn");
}
Bar.h
typedef struct Foo Foo;
struct Foo {
int number;
} MYFOO = {2};
void helloWorld(void);
Foo.c
#include "bar.h"
int main() {
helloWorld();
}
Interestingly enough, when I remove the line containing
MYFOO = {2};
The code compiles and works perfectly fine. I believe it has to do with including Bar.h twice, which ends up including that struct twice? But how would I avoid something like that?
Thank you!
c gcc
add a comment |
I am trying to create a struct in a header file, and initialize a template struct. For some reason, when including the header file in multiple files, it gives me the following error:
gcc foo.c bar.c -o foo -Wall
duplicate symbol _MYFOO in:
/var/folders/s4/zyw5lgk92wj9ljnsypgwdccr0000gn/T/foo-52f8fc.o
/var/folders/s4/zyw5lgk92wj9ljnsypgwdccr0000gn/T/bar-6dc21f.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
These are my files:
Bar.c
:
#include "bar.h"
#include <stdio.h>
void helloWorld() {
printf("Hello worldn");
}
Bar.h
typedef struct Foo Foo;
struct Foo {
int number;
} MYFOO = {2};
void helloWorld(void);
Foo.c
#include "bar.h"
int main() {
helloWorld();
}
Interestingly enough, when I remove the line containing
MYFOO = {2};
The code compiles and works perfectly fine. I believe it has to do with including Bar.h twice, which ends up including that struct twice? But how would I avoid something like that?
Thank you!
c gcc
I am trying to create a struct in a header file, and initialize a template struct. For some reason, when including the header file in multiple files, it gives me the following error:
gcc foo.c bar.c -o foo -Wall
duplicate symbol _MYFOO in:
/var/folders/s4/zyw5lgk92wj9ljnsypgwdccr0000gn/T/foo-52f8fc.o
/var/folders/s4/zyw5lgk92wj9ljnsypgwdccr0000gn/T/bar-6dc21f.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
These are my files:
Bar.c
:
#include "bar.h"
#include <stdio.h>
void helloWorld() {
printf("Hello worldn");
}
Bar.h
typedef struct Foo Foo;
struct Foo {
int number;
} MYFOO = {2};
void helloWorld(void);
Foo.c
#include "bar.h"
int main() {
helloWorld();
}
Interestingly enough, when I remove the line containing
MYFOO = {2};
The code compiles and works perfectly fine. I believe it has to do with including Bar.h twice, which ends up including that struct twice? But how would I avoid something like that?
Thank you!
c gcc
c gcc
edited Nov 22 '18 at 1:34
Joseph Farah
1,42311627
1,42311627
asked Nov 22 '18 at 1:32
OtananOtanan
329110
329110
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You could add a directive to the Bar.h file to check if the file has already been included:
#ifndef _BAR_H_INCLUDED_
// Bar.h not included - declare your structs, etc, here.
// Define _BAR_H_INCLUDED_ to indicate this file has already
// been included
#define _BAR_H_INCLUDED_ 1
#endif
This should at least prevent you including Bar.h
multiple times.
EDIT
A better solution might be to include the Bar.c
from within the Bar.h
:
// Bar.h
#ifndef _BAR_C_INCLUDED_
// code here
// Include Bar.c
#include "Bar.c"
#define _BAR_C_INCLUDED_
#endif
You can then simply include Bar.h
in your Foo.c
:
// Foo.c
#include <stdio.h>
#include <stdlib.h>
#include "Bar.h"
int main() {
//...
Then to compile:
gcc Foo.c -o Foo
So - here is your updated code - first, Bar.h
#ifndef _BAR_C_INCLUDED_
typedef struct Foo Foo;
struct Foo {
int number;
} MYFOO = {2};
void helloWorld (void);
#include "Bar.c"
#define _BAR_C_INCLUDED_
#endif
Now Bar.c
:
void helloWorld() {
printf("Hello worldn");
}
Lastly, Foo.c
- include stdio.h
here as well as Bar.h
(which will, in turn, include Bar.c
for us):
#include <stdio.h>
#include "bar.h"
int main() {
helloWorld();
}
And to compile:
gcc Foo.c -o Foo -Wall
Thank you for the answer! I'm a little confused, since bar.h includes the function prototype, I need to include it in bar.c to define it, and I need to also include it in foo.c to use it? Am I understanding that correctly? Is there an alternative?
– Otanan
Nov 22 '18 at 1:41
1
Well, you are un-necessarily including Bar.h twice, what you should probably do is include Bar.c from witihin Bar.h - I'll update.
– Nunchy
Nov 22 '18 at 1:52
1
Hmmm...did you alter thegcc
compile command? You were intially includingBar.c
the first time around, but you don't have to sinceBar.h
is including it for you.
– Nunchy
Nov 22 '18 at 2:14
1
Glad to help, have a good day!
– Nunchy
Nov 22 '18 at 2:23
1
No. A better solution is to learn how the build process works in the C ecosystem. Including implementation files is not the way to go.
– StoryTeller
Nov 22 '18 at 5:03
add a comment |
After toying around some more, I found the reason for the error coming from the line MYFOO = {2};
It had to do with the fact that I was initializing the struct in my header file.
Header files are meant for definitions, not initializations.
Instead, the solution for the problem was to simply define and initialize the line in the corresponding source file Foo.c
.
Now, in that file I included as a global variable:
Foo MYFOO = {2};
Now to access this variable in any other file, such as in my Bar.c
, all I needed to do was include the line,
extern Foo MYFOO;
This solved my problem for compilation and meant that I could use the struct in other files as desired!
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422711%2fc-duplicate-symbols-for-architecture-x86-64%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could add a directive to the Bar.h file to check if the file has already been included:
#ifndef _BAR_H_INCLUDED_
// Bar.h not included - declare your structs, etc, here.
// Define _BAR_H_INCLUDED_ to indicate this file has already
// been included
#define _BAR_H_INCLUDED_ 1
#endif
This should at least prevent you including Bar.h
multiple times.
EDIT
A better solution might be to include the Bar.c
from within the Bar.h
:
// Bar.h
#ifndef _BAR_C_INCLUDED_
// code here
// Include Bar.c
#include "Bar.c"
#define _BAR_C_INCLUDED_
#endif
You can then simply include Bar.h
in your Foo.c
:
// Foo.c
#include <stdio.h>
#include <stdlib.h>
#include "Bar.h"
int main() {
//...
Then to compile:
gcc Foo.c -o Foo
So - here is your updated code - first, Bar.h
#ifndef _BAR_C_INCLUDED_
typedef struct Foo Foo;
struct Foo {
int number;
} MYFOO = {2};
void helloWorld (void);
#include "Bar.c"
#define _BAR_C_INCLUDED_
#endif
Now Bar.c
:
void helloWorld() {
printf("Hello worldn");
}
Lastly, Foo.c
- include stdio.h
here as well as Bar.h
(which will, in turn, include Bar.c
for us):
#include <stdio.h>
#include "bar.h"
int main() {
helloWorld();
}
And to compile:
gcc Foo.c -o Foo -Wall
Thank you for the answer! I'm a little confused, since bar.h includes the function prototype, I need to include it in bar.c to define it, and I need to also include it in foo.c to use it? Am I understanding that correctly? Is there an alternative?
– Otanan
Nov 22 '18 at 1:41
1
Well, you are un-necessarily including Bar.h twice, what you should probably do is include Bar.c from witihin Bar.h - I'll update.
– Nunchy
Nov 22 '18 at 1:52
1
Hmmm...did you alter thegcc
compile command? You were intially includingBar.c
the first time around, but you don't have to sinceBar.h
is including it for you.
– Nunchy
Nov 22 '18 at 2:14
1
Glad to help, have a good day!
– Nunchy
Nov 22 '18 at 2:23
1
No. A better solution is to learn how the build process works in the C ecosystem. Including implementation files is not the way to go.
– StoryTeller
Nov 22 '18 at 5:03
add a comment |
You could add a directive to the Bar.h file to check if the file has already been included:
#ifndef _BAR_H_INCLUDED_
// Bar.h not included - declare your structs, etc, here.
// Define _BAR_H_INCLUDED_ to indicate this file has already
// been included
#define _BAR_H_INCLUDED_ 1
#endif
This should at least prevent you including Bar.h
multiple times.
EDIT
A better solution might be to include the Bar.c
from within the Bar.h
:
// Bar.h
#ifndef _BAR_C_INCLUDED_
// code here
// Include Bar.c
#include "Bar.c"
#define _BAR_C_INCLUDED_
#endif
You can then simply include Bar.h
in your Foo.c
:
// Foo.c
#include <stdio.h>
#include <stdlib.h>
#include "Bar.h"
int main() {
//...
Then to compile:
gcc Foo.c -o Foo
So - here is your updated code - first, Bar.h
#ifndef _BAR_C_INCLUDED_
typedef struct Foo Foo;
struct Foo {
int number;
} MYFOO = {2};
void helloWorld (void);
#include "Bar.c"
#define _BAR_C_INCLUDED_
#endif
Now Bar.c
:
void helloWorld() {
printf("Hello worldn");
}
Lastly, Foo.c
- include stdio.h
here as well as Bar.h
(which will, in turn, include Bar.c
for us):
#include <stdio.h>
#include "bar.h"
int main() {
helloWorld();
}
And to compile:
gcc Foo.c -o Foo -Wall
Thank you for the answer! I'm a little confused, since bar.h includes the function prototype, I need to include it in bar.c to define it, and I need to also include it in foo.c to use it? Am I understanding that correctly? Is there an alternative?
– Otanan
Nov 22 '18 at 1:41
1
Well, you are un-necessarily including Bar.h twice, what you should probably do is include Bar.c from witihin Bar.h - I'll update.
– Nunchy
Nov 22 '18 at 1:52
1
Hmmm...did you alter thegcc
compile command? You were intially includingBar.c
the first time around, but you don't have to sinceBar.h
is including it for you.
– Nunchy
Nov 22 '18 at 2:14
1
Glad to help, have a good day!
– Nunchy
Nov 22 '18 at 2:23
1
No. A better solution is to learn how the build process works in the C ecosystem. Including implementation files is not the way to go.
– StoryTeller
Nov 22 '18 at 5:03
add a comment |
You could add a directive to the Bar.h file to check if the file has already been included:
#ifndef _BAR_H_INCLUDED_
// Bar.h not included - declare your structs, etc, here.
// Define _BAR_H_INCLUDED_ to indicate this file has already
// been included
#define _BAR_H_INCLUDED_ 1
#endif
This should at least prevent you including Bar.h
multiple times.
EDIT
A better solution might be to include the Bar.c
from within the Bar.h
:
// Bar.h
#ifndef _BAR_C_INCLUDED_
// code here
// Include Bar.c
#include "Bar.c"
#define _BAR_C_INCLUDED_
#endif
You can then simply include Bar.h
in your Foo.c
:
// Foo.c
#include <stdio.h>
#include <stdlib.h>
#include "Bar.h"
int main() {
//...
Then to compile:
gcc Foo.c -o Foo
So - here is your updated code - first, Bar.h
#ifndef _BAR_C_INCLUDED_
typedef struct Foo Foo;
struct Foo {
int number;
} MYFOO = {2};
void helloWorld (void);
#include "Bar.c"
#define _BAR_C_INCLUDED_
#endif
Now Bar.c
:
void helloWorld() {
printf("Hello worldn");
}
Lastly, Foo.c
- include stdio.h
here as well as Bar.h
(which will, in turn, include Bar.c
for us):
#include <stdio.h>
#include "bar.h"
int main() {
helloWorld();
}
And to compile:
gcc Foo.c -o Foo -Wall
You could add a directive to the Bar.h file to check if the file has already been included:
#ifndef _BAR_H_INCLUDED_
// Bar.h not included - declare your structs, etc, here.
// Define _BAR_H_INCLUDED_ to indicate this file has already
// been included
#define _BAR_H_INCLUDED_ 1
#endif
This should at least prevent you including Bar.h
multiple times.
EDIT
A better solution might be to include the Bar.c
from within the Bar.h
:
// Bar.h
#ifndef _BAR_C_INCLUDED_
// code here
// Include Bar.c
#include "Bar.c"
#define _BAR_C_INCLUDED_
#endif
You can then simply include Bar.h
in your Foo.c
:
// Foo.c
#include <stdio.h>
#include <stdlib.h>
#include "Bar.h"
int main() {
//...
Then to compile:
gcc Foo.c -o Foo
So - here is your updated code - first, Bar.h
#ifndef _BAR_C_INCLUDED_
typedef struct Foo Foo;
struct Foo {
int number;
} MYFOO = {2};
void helloWorld (void);
#include "Bar.c"
#define _BAR_C_INCLUDED_
#endif
Now Bar.c
:
void helloWorld() {
printf("Hello worldn");
}
Lastly, Foo.c
- include stdio.h
here as well as Bar.h
(which will, in turn, include Bar.c
for us):
#include <stdio.h>
#include "bar.h"
int main() {
helloWorld();
}
And to compile:
gcc Foo.c -o Foo -Wall
edited Nov 22 '18 at 2:20
answered Nov 22 '18 at 1:38
NunchyNunchy
825411
825411
Thank you for the answer! I'm a little confused, since bar.h includes the function prototype, I need to include it in bar.c to define it, and I need to also include it in foo.c to use it? Am I understanding that correctly? Is there an alternative?
– Otanan
Nov 22 '18 at 1:41
1
Well, you are un-necessarily including Bar.h twice, what you should probably do is include Bar.c from witihin Bar.h - I'll update.
– Nunchy
Nov 22 '18 at 1:52
1
Hmmm...did you alter thegcc
compile command? You were intially includingBar.c
the first time around, but you don't have to sinceBar.h
is including it for you.
– Nunchy
Nov 22 '18 at 2:14
1
Glad to help, have a good day!
– Nunchy
Nov 22 '18 at 2:23
1
No. A better solution is to learn how the build process works in the C ecosystem. Including implementation files is not the way to go.
– StoryTeller
Nov 22 '18 at 5:03
add a comment |
Thank you for the answer! I'm a little confused, since bar.h includes the function prototype, I need to include it in bar.c to define it, and I need to also include it in foo.c to use it? Am I understanding that correctly? Is there an alternative?
– Otanan
Nov 22 '18 at 1:41
1
Well, you are un-necessarily including Bar.h twice, what you should probably do is include Bar.c from witihin Bar.h - I'll update.
– Nunchy
Nov 22 '18 at 1:52
1
Hmmm...did you alter thegcc
compile command? You were intially includingBar.c
the first time around, but you don't have to sinceBar.h
is including it for you.
– Nunchy
Nov 22 '18 at 2:14
1
Glad to help, have a good day!
– Nunchy
Nov 22 '18 at 2:23
1
No. A better solution is to learn how the build process works in the C ecosystem. Including implementation files is not the way to go.
– StoryTeller
Nov 22 '18 at 5:03
Thank you for the answer! I'm a little confused, since bar.h includes the function prototype, I need to include it in bar.c to define it, and I need to also include it in foo.c to use it? Am I understanding that correctly? Is there an alternative?
– Otanan
Nov 22 '18 at 1:41
Thank you for the answer! I'm a little confused, since bar.h includes the function prototype, I need to include it in bar.c to define it, and I need to also include it in foo.c to use it? Am I understanding that correctly? Is there an alternative?
– Otanan
Nov 22 '18 at 1:41
1
1
Well, you are un-necessarily including Bar.h twice, what you should probably do is include Bar.c from witihin Bar.h - I'll update.
– Nunchy
Nov 22 '18 at 1:52
Well, you are un-necessarily including Bar.h twice, what you should probably do is include Bar.c from witihin Bar.h - I'll update.
– Nunchy
Nov 22 '18 at 1:52
1
1
Hmmm...did you alter the
gcc
compile command? You were intially including Bar.c
the first time around, but you don't have to since Bar.h
is including it for you.– Nunchy
Nov 22 '18 at 2:14
Hmmm...did you alter the
gcc
compile command? You were intially including Bar.c
the first time around, but you don't have to since Bar.h
is including it for you.– Nunchy
Nov 22 '18 at 2:14
1
1
Glad to help, have a good day!
– Nunchy
Nov 22 '18 at 2:23
Glad to help, have a good day!
– Nunchy
Nov 22 '18 at 2:23
1
1
No. A better solution is to learn how the build process works in the C ecosystem. Including implementation files is not the way to go.
– StoryTeller
Nov 22 '18 at 5:03
No. A better solution is to learn how the build process works in the C ecosystem. Including implementation files is not the way to go.
– StoryTeller
Nov 22 '18 at 5:03
add a comment |
After toying around some more, I found the reason for the error coming from the line MYFOO = {2};
It had to do with the fact that I was initializing the struct in my header file.
Header files are meant for definitions, not initializations.
Instead, the solution for the problem was to simply define and initialize the line in the corresponding source file Foo.c
.
Now, in that file I included as a global variable:
Foo MYFOO = {2};
Now to access this variable in any other file, such as in my Bar.c
, all I needed to do was include the line,
extern Foo MYFOO;
This solved my problem for compilation and meant that I could use the struct in other files as desired!
add a comment |
After toying around some more, I found the reason for the error coming from the line MYFOO = {2};
It had to do with the fact that I was initializing the struct in my header file.
Header files are meant for definitions, not initializations.
Instead, the solution for the problem was to simply define and initialize the line in the corresponding source file Foo.c
.
Now, in that file I included as a global variable:
Foo MYFOO = {2};
Now to access this variable in any other file, such as in my Bar.c
, all I needed to do was include the line,
extern Foo MYFOO;
This solved my problem for compilation and meant that I could use the struct in other files as desired!
add a comment |
After toying around some more, I found the reason for the error coming from the line MYFOO = {2};
It had to do with the fact that I was initializing the struct in my header file.
Header files are meant for definitions, not initializations.
Instead, the solution for the problem was to simply define and initialize the line in the corresponding source file Foo.c
.
Now, in that file I included as a global variable:
Foo MYFOO = {2};
Now to access this variable in any other file, such as in my Bar.c
, all I needed to do was include the line,
extern Foo MYFOO;
This solved my problem for compilation and meant that I could use the struct in other files as desired!
After toying around some more, I found the reason for the error coming from the line MYFOO = {2};
It had to do with the fact that I was initializing the struct in my header file.
Header files are meant for definitions, not initializations.
Instead, the solution for the problem was to simply define and initialize the line in the corresponding source file Foo.c
.
Now, in that file I included as a global variable:
Foo MYFOO = {2};
Now to access this variable in any other file, such as in my Bar.c
, all I needed to do was include the line,
extern Foo MYFOO;
This solved my problem for compilation and meant that I could use the struct in other files as desired!
answered Dec 2 '18 at 19:31
OtananOtanan
329110
329110
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422711%2fc-duplicate-symbols-for-architecture-x86-64%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown