我在一個簡單的C代碼中做了一個平庸的行爲,我正在爲教育目的而做。鐺如果至少沒有使用-O2,連接器就會失敗
如果我使用低於-O2的東西進行編譯,它會在鏈接編輯期間中斷此輸出。
$ make
clang -Wall -march=native -pipe -c -g -D_DEBUG_ main.c
clang -Wall -march=native -pipe -c -g -D_DEBUG_ functions.c
clang -Wall -o main main.o functions.o
Undefined symbols for architecture x86_64:
"_getbit", referenced from:
_getValueFromMatrix in functions.o
"_setbit", referenced from:
_populateMatrix in functions.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [main] Error 1
我不知道這是否有幫助,但是,這裏是執行setbit();和getbit();
inline void setbit(uint64_t *inteiro, unsigned char pos) {
*(uint64_t*)inteiro |= (uint64_t)1 << pos;
}
inline bool getbit(uint64_t inteiro, unsigned char pos) {
return (inteiro & ((uint64_t)1 << pos));
}
編輯:
functions.h
#ifndef __FUNCTIONS_H__
#define __FUNCTIONS_H__
/* Funções para manipulação de bits */
inline void setbit(uint64_t *, unsigned char);
inline void clearbit(uint64_t *, unsigned char);
inline bool getbit(uint64_t, unsigned char);
inline unsigned char getbitChar(uint64_t, unsigned char);
char *uint64_t2bin(uint64_t, char *, int);
#endif
包括main.c中
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "errors.h"
#include "const.h"
#include "types.h"
#include "functions.h"
setbit和getbit在main.c或functions.c中定義?或者在functions.c中包含的其中一個頭文件中? –
是的,它們在functions.h中: inline void setbit(uint64_t *,unsigned char); inline bool getbit(uint64_t,unsigned char); –
這些函數的實現在哪裏定義?我想任何人給你一個明確的答案,你需要顯示所有的代碼。但似乎編譯器沒有看到setbit和getbit的定義。也許它們不在你給編譯器的文件中,也許它們被一些#ifdefs隱藏,但是編譯器沒有看到setbit和getbit的定義。 –