为了教育目的,我在一个简单的 C 代码中有一个奇怪的行为。
如果我用低于 -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));
}
编辑:
函数.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"