Mat*_*res 4 c linker compiler-errors header-files
我在flag.h中定义了一些常量,所以link.c和linkedlist.h可以使用它.但是当我编译时:
clang -Wall main.c link.c linkedlist.c
我得到以下内容
/tmp/linkedlist-o2mcAI.o:(.rodata+0x0):`的多个定义VARIABLE_NAME '
/tmp/link-oXhyfE.o:(.rodata+0x0):首先在这里定义
从所有变量flag.h中使用link.c和linkedlist.c,并在最后:
clang:错误:链接器命令失败,退出代码为1(使用-v查看调用)
main.c,flag.h,link.h,link.c,linkedlist.h和linkedlist.c的示例代码:
#include <stdio.h>
#include "linkedlist.h"
int main(void) {
LinkedList* list = LinkedList_new();
}
Run Code Online (Sandbox Code Playgroud)
#ifndef FLAG_H_
#define FLAG_H_
#include <limits.h>
#define FALSE 0
#define TRUE 1
const int OK = 1;
const int ERROR = -1;
const int FLAG = 0;
// other constants
#endif
Run Code Online (Sandbox Code Playgroud)
#ifndef LINK_H_
#define LINK_H_
typedef struct Link {
int value;
struct Link* next;
} Link;
Link* Link_new(int value);
int useExample(int value);
// other methods
#endif
Run Code Online (Sandbox Code Playgroud)
#include <stdio.h>
#include <stdlib.h>
#include "link.h"
#include "flag.h"
Link* Link_new(int value)
{
Link* link = malloc(sizeof(Link));
link->value = value;
link->next = NULL;
return link;
}
useExample(int value)
{
if (value == 0) {
return OK; // defined in flag.h
} else {
return FLAG; // defined in flag.h
}
}
// other methods
Run Code Online (Sandbox Code Playgroud)
#ifndef LINKEDLIST_H_
#define LINKEDLIST_H_
#include "link.h"
typedef struct LinkedList {
Link* first;
unsigned int size;
} LinkedList;
LinkedList* LinkedList_new();
int anotherUseExample(int value);
// other methods
#endif
Run Code Online (Sandbox Code Playgroud)
#include <stdio.h>
#include <stdlib.h>
#include "linkedlist.h"
#include "flag.h"
LinkedList* LinkedList_new() {
LinkedList* list = malloc(sizeof(LinkedList));
list->first = NULL;
list->size = 0;
return list;
}
anotherUseExample(int value)
{
if (value == 0) {
return FLAG; // defined in flag.h
} else {
return ERROR; // defined in flag.h
}
}
// other methods
Run Code Online (Sandbox Code Playgroud)
那么如何在link.c和linkedlist.c中使用flag.h而不会出现多重定义?
而且...我编码头文件和编译的方式是正确的?
完整输出-v:
clang version 3.3 (tags/RELEASE_33/rc3)
Target: i386-redhat-linux-gnu
Thread model: posix
"/usr/bin/clang" -cc1 -triple i386-redhat-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name main.c -mrelocation-model static -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -fuse-init-array -target-cpu pentium4 -target-linker-version 2.23.52.0.1 -v -resource-dir /usr/bin/../lib/clang/3.3 -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib/clang/3.3/include -internal-externc-isystem /usr/include -internal-externc-isystem /usr/lib/gcc/i686-redhat-linux/4.8.1/include -Wall -fdebug-compilation-dir /home/jharvard/Desktop/Code/LinkedList -ferror-limit 19 -fmessage-length 80 -mstackrealign -fobjc-runtime=gcc -fobjc-default-synthesize-properties -fdiagnostics-show-option -fcolor-diagnostics -backend-option -vectorize-loops -o /tmp/main-JmZTmN.o -x c main.c
clang -cc1 version 3.3 based upon LLVM 3.3 default target i386-redhat-linux-gnu
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/usr/bin/../lib/clang/3.3/include
/usr/include
/usr/lib/gcc/i686-redhat-linux/4.8.1/include
End of search list.
"/usr/bin/clang" -cc1 -triple i386-redhat-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name link.c -mrelocation-model static -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -fuse-init-array -target-cpu pentium4 -target-linker-version 2.23.52.0.1 -v -resource-dir /usr/bin/../lib/clang/3.3 -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib/clang/3.3/include -internal-externc-isystem /usr/include -internal-externc-isystem /usr/lib/gcc/i686-redhat-linux/4.8.1/include -Wall -fdebug-compilation-dir /home/jharvard/Desktop/Code/LinkedList -ferror-limit 19 -fmessage-length 80 -mstackrealign -fobjc-runtime=gcc -fobjc-default-synthesize-properties -fdiagnostics-show-option -fcolor-diagnostics -backend-option -vectorize-loops -o /tmp/link-FtygcZ.o -x c link.c
clang -cc1 version 3.3 based upon LLVM 3.3 default target i386-redhat-linux-gnu
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/usr/bin/../lib/clang/3.3/include
/usr/include
/usr/lib/gcc/i686-redhat-linux/4.8.1/include
End of search list.
"/usr/bin/clang" -cc1 -triple i386-redhat-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name linkedlist.c -mrelocation-model static -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -fuse-init-array -target-cpu pentium4 -target-linker-version 2.23.52.0.1 -v -resource-dir /usr/bin/../lib/clang/3.3 -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib/clang/3.3/include -internal-externc-isystem /usr/include -internal-externc-isystem /usr/lib/gcc/i686-redhat-linux/4.8.1/include -Wall -fdebug-compilation-dir /home/jharvard/Desktop/Code/LinkedList -ferror-limit 19 -fmessage-length 80 -mstackrealign -fobjc-runtime=gcc -fobjc-default-synthesize-properties -fdiagnostics-show-option -fcolor-diagnostics -backend-option -vectorize-loops -o /tmp/linkedlist-n0zF1a.o -x c linkedlist.c
clang -cc1 version 3.3 based upon LLVM 3.3 default target i386-redhat-linux-gnu
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/usr/bin/../lib/clang/3.3/include
/usr/include
/usr/lib/gcc/i686-redhat-linux/4.8.1/include
End of search list.
"/usr/bin/ld" --eh-frame-hdr -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 -o a.out /usr/lib/gcc/i686-redhat-linux/4.8.1/../../../crt1.o /usr/lib/gcc/i686-redhat-linux/4.8.1/../../../crti.o /usr/lib/gcc/i686-redhat-linux/4.8.1/crtbegin.o -L/usr/lib/gcc/i686-redhat-linux/4.8.1 -L/usr/lib/gcc/i686-redhat-linux/4.8.1/../../.. -L/lib -L/usr/lib /tmp/main-JmZTmN.o /tmp/link-FtygcZ.o /tmp/linkedlist-n0zF1a.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/i686-redhat-linux/4.8.1/crtend.o /usr/lib/gcc/i686-redhat-linux/4.8.1/../../../crtn.o
/tmp/linkedlist-n0zF1a.o:(.rodata+0x4): multiple definition of `ERROR_indexOutOfBounds'
/tmp/link-FtygcZ.o:(.rodata+0x4): first defined here
/tmp/linkedlist-n0zF1a.o:(.rodata+0x8): multiple definition of `ERROR_invalidArgument'
/tmp/link-FtygcZ.o:(.rodata+0x8): first defined here
/tmp/linkedlist-n0zF1a.o:(.rodata+0x10): multiple definition of `FLAG_notFound'
/tmp/link-FtygcZ.o:(.rodata+0x10): first defined here
/tmp/linkedlist-n0zF1a.o:(.rodata+0xc): multiple definition of `FLAG_undefined'
/tmp/link-FtygcZ.o:(.rodata+0xc): first defined here
/tmp/linkedlist-n0zF1a.o:(.rodata+0x0): multiple definition of `OK'
/tmp/link-FtygcZ.o:(.rodata+0x0): first defined here
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Run Code Online (Sandbox Code Playgroud)
Ida*_*rye 13
#includeC中的指令只是复制头文件中的文本.这意味着,当你编译既link.c和linkedlist.c,从常量定义flag.h被复制到两个,现在所有这些常量在这两个定义link.o和linkedlist.o`.当您链接程序时,您会得到一个名称,您会收到多重定义错误.
你需要从定义中拆分声明,并创建一个flag.c你定义的文件const int OK = 1;等等,然后flag.h你将继续使用const int OK;等.这样,常量将编译成一个单独的目标文件 - flag.o- 你将不会得到链接时出现多重定义错误.
标志.h:
extern const int OK, ERROR, FLAG;
Run Code Online (Sandbox Code Playgroud)
标志.c:
const int OK = 1;
const int ERROR = -1;
const int FLAG = 0;
Run Code Online (Sandbox Code Playgroud)
由于您不需要将这些常量作为对象,因此您应该使用其他功能来定义它们,即枚举.
enum { OK = 1, ERROR = -1, FLAG = 0, ONE = 1, };
Run Code Online (Sandbox Code Playgroud)
这些都是类型的int,永远不会产生多个符号错误.它们的优点是它们可以保留在.h文件中.因此,所有使用它们的功能都可以看到值,并且可以更好地进行优化.
从上面的示例中可以看出,值不必按顺序出现,并且相同的值可能会出现多次.
| 归档时间: |
|
| 查看次数: |
10711 次 |
| 最近记录: |