英文:
error caused by boolean on C( I'm a beginner so please try to be simple)
问题
I'm trying to use boolean but for some reason, it just gives me an error on Clion.
#include <stdio.h>
#define bool int
#define true 1
#define false 0
int main() {
bool isf = true;
bool ift = false;
printf("%d\n", isf);
printf("%d\n", ift);
}
error code: clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
I tried using
#include <stdbool.h>
but it just ignores it even though I'm on C17.
I also tried defining them which did show that it's not wrong but it showed me an error when I ran it.
I also use Mac and I have Xcode.
I also started learning C a couple of days ago, so please bear with me.
Correct error code:
duplicate symbol '_main' in:
CMakeFiles/untitled3.dir/main.c.o
CMakeFiles/untitled3.dir/Second_class.c.o
ld: 1 duplicate symbol for architecture arm64.
英文:
I'm trying to use boolean but for some reason, it just gives me an error on Clion.
#include <stdio.h>
#define bool int
#define true 1
#define false 0
int main() {
bool isf = true;
bool ift = false;
printf("%d\n", isf);
printf("%d\n", ift);
}
error code: clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
I tried using
#include <stdbool.h>
but it just ignores it even though I'm on C17.
I also tried defining them which did show that it's not wrong but it showed me an error when I ran it.
I also use Mac and I have Xcode.
I also started learning C a couple days ago so please bear with me.
Correct error code:
duplicate symbol '_main' in:
CMakeFiles/untitled3.dir/main.c.o
CMakeFiles/untitled3.dir/Second_class.c.o
ld: 1 duplicate symbol for architecture arm64.
答案1
得分: 0
根据错误消息,您正在链接两个文件CMakeFiles/untitled3.dir/main.c.o
和CMakeFiles/untitled3.dir/Second_class.c.o
,它们都定义了符号_main
。
- 您需要要么不将这两个文件链接到同一个可执行文件中,或者
- 将其中一个符号更改为不同的名称
@RetiredNinja说您有两个main()
函数。当我尝试在Linux上重现时,出现了不同的错误:
$ clang 1.o 2.o -o 3|& grep main
/usr/bin/ld: 2.o: in function `main':
(.text+0xf0): multiple definition of `main'; 1.o:(.text+0xf0): first defined here
英文:
As indicated by the error message you are linking two files CMakeFiles/untitled3.dir/main.c.o
and CMakeFiles/untitled3.dir/Second_class.c.o
that both define the symbol _main
.
- You need to either not link both files into the same executable, or
- Change one of those symbols to a different name
@RetiredNinja says you have two main()
functions. I get a differenet error on Linux when I tried to reproduce it:
$ clang 1.o 2.o -o 3|& grep main
/usr/bin/ld: 2.o: in function `main':
(.text+0xf0): multiple definition of `main'; 1.o:(.text+0xf0): first defined here
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论