简体   繁体   English

如果至少未使用-O2,则clang Linker失败

[英]clang Linker fails if at least -O2 wasn't used

I've an strage behaviour in a simple C code that I'm doing for educational purposes. 我在出于教育目的的简单C代码中有一种举止行为。

If I compile it with something lower than -O2 it breaks during link-edition with this output. 如果我用低于-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

I don't know if this helps but, here are the implementation of setbit(); 我不知道这是否有帮助,但是,这是setbit()的实现; and getbit(); 和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));
}

EDIT: 编辑:

functions.h 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

Includes in main.c 包含在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"

inline only is the right thing to use, if there is a definition of the function in the .h file. 如果.h文件中有函数的定义,则仅使用inline是正确的选择。 It basically tells the compiler that it shouldn't produce a code for the function in every compilation unit (your .c files). 它基本上告诉编译器不应在每个编译单元(您的.c文件)中为该函数生成代码。

If there is no such definition in the .h file, as it seems here, just don't use inline at all, it just makes no sense. 如果.h文件中没有这样的定义(如此处所示),则根本不使用inline ,这毫无意义。

If your worry is the efficiency of the other functions in the unit where you define your inline functions, you really don't need that. 如果您担心定义inline函数的单元中其他函数的效率,则实际上不需要。 The compiler will inline any function that it has its hands on and where its criteria say that it is worth to do so. 编译器将内联任何可以使用的函数,并且在其条件认为值得这样做的地方。

If you really want to put the definitions to appear in the header file such that all units can see the definition, then use inline . 如果你真想把定义出现在头文件,使得各单位可以看到的定义, 然后inline In that case you'd have to include an "instantiation" of your function in just one unit, to ensure that the code is issued exactly once: 在这种情况下,您必须仅在一个单元中包含函数的“实例化”,以确保代码恰好发出一次:

extern inline void setbit(uint64_t *, unsigned char);
extern inline void clearbit(uint64_t *, unsigned char);
extern inline bool getbit(uint64_t, unsigned char);
extern inline unsigned char getbitChar(uint64_t, unsigned char);

The inline functions don't have any external definition, so when the compiler fails to inline them (which it won't do at -O0 ), the linker fails to find a definition, and an error results. 内联函数没有任何外部定义,因此当编译器无法内联它们时(在-O0处不会执行),链接器将找不到定义,并产生错误。 The easiest fix is to change inline to static inline . 最简单的解决方法是将inline更改为static inline Non-static inline is hard to use, confusing, and usually not useful. 非静态内联很难使用,引起混乱,并且通常没有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM