error caused by boolean on C( I'm a beginner so please try to be simple)

huangapple go评论73阅读模式
英文:

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 &lt;stdio.h&gt;

#define bool int
#define true 1
#define false 0
int main() {
    bool isf = true;
    bool ift = false;
    printf(&quot;%d\n&quot;, isf);
    printf(&quot;%d\n&quot;, 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 &lt;stdbool.h&gt; 

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 &#39;_main&#39; 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.oCMakeFiles/untitled3.dir/Second_class.c.o,它们都定义了符号_main

  1. 您需要要么不将这两个文件链接到同一个可执行文件中,或者
  2. 将其中一个符号更改为不同的名称

@RetiredNinja说您有两个main()函数。当我尝试在Linux上重现时,出现了不同的错误:

$ clang 1.o 2.o -o 3|&amp; grep main
/usr/bin/ld: 2.o: in function `main&#39;:
(.text+0xf0): multiple definition of `main&#39;; 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.

  1. You need to either not link both files into the same executable, or
  2. 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|&amp; grep main
/usr/bin/ld: 2.o: in function `main&#39;:
(.text+0xf0): multiple definition of `main&#39;; 1.o:(.text+0xf0): first defined here

huangapple
  • 本文由 发表于 2023年4月20日 07:31:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76059523.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定