如果至少未使用-O2,则clang Linker失败 [英] clang Linker fails if at least -O2 wasn't used

查看:100
本文介绍了如果至少未使用-O2,则clang Linker失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在出于教育目的的简单C代码中有一种奇怪的行为。

I've an strage behaviour in a simple C code that I'm doing for educational purposes.

如果我用低于-O2的值进行编译,则会中断

If I compile it with something lower than -O2 it breaks during link-edition with this output.

$ 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();

I don't know if this helps but, here are the implementation of setbit(); and 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

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"


推荐答案

内联如果.h文件中有函数的定义,则仅使用正确的方法即可。它基本上告诉编译器不应在每个编译单元(您的.c文件)中为该函数生成代码。

inline only is the right thing to use, if there is a definition of the function in the .h file. It basically tells the compiler that it shouldn't produce a code for the function in every compilation unit (your .c files).

如果.h文件,看起来好像根本不使用 inline ,这完全没有意义。

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.

如果您担心的是在定义内联函数的单元中其他函数的效率,则确实不需要。

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. The compiler will inline any function that it has its hands on and where its criteria say that it is worth to do so.

如果您真的想显示定义,则编译器将内联它可以使用的任何函数,并在其标准认为值得这样做的地方进行内联。在头文件中,以便所有单位都可以看到定义,然后使用 inline 。在那种情况下,您必须在一个单元中包含函数的实例化,以确保代码恰好发出一次:

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. 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);

这篇关于如果至少未使用-O2,则clang Linker失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆