我正在用C99编写一个库,并且库的某些部分可以从使用宏/内联函数中获益.内联函数更适合我的库.
但是,非常具体地说我不想在外部公开这些内联函数.
一切似乎都有效,但当我链接到库以创建可执行文件时,我得到错误:"未定义引用`na_impl_gfx__draw'"
我已经将问题复制到一个最小的测试用例,它完全符合我的要求:
lib.h:
void somefunc();
Run Code Online (Sandbox Code Playgroud)
lib.c:
#include <stdio.h>
#include "lib.h"
inline void someinline(char *value);
void somefunc() {
someinline("Hi!");
}
inline void someinline(char *value) {
printf("%s\n", value);
}
Run Code Online (Sandbox Code Playgroud)
main.c中:
#include "lib.h"
int main(int argc, char *argv[]) {
somefunc();
}
Run Code Online (Sandbox Code Playgroud)
现在我们编译:
doug@Wulf:~/test$ gcc -c -std=c99 lib.c
doug@Wulf:~/test$ gcc -c -std=c99 main.c
doug@Wulf:~/test$ gcc -std=c99 lib.o main.o
lib.o: In function `somefunc':
lib.c:(.text+0xe): undefined reference to `someinline'
lib.c:(.text+0x1a): undefined reference to `someinline'
lib.c:(.text+0x26): undefined reference to `someinline'
collect2: ld returned 1 exit …Run Code Online (Sandbox Code Playgroud) 如果我有两个结构:
struct A
{
float x, y;
inline A operator*(A b)
{
A out;
out.x = x * b.x;
out.y = y * b.y;
return out;
}
}
Run Code Online (Sandbox Code Playgroud)
和一个等效的结构
struct B
{
float x, y;
}
inline B operator*(B a, B b)
{
B out;
out.x = a.x * b.x;
out.y = a.y * b.y;
return out;
}
Run Code Online (Sandbox Code Playgroud)
您是否知道B的运算符*有任何不同的编译原因,或者运行速度比A的运算符更慢或更快*(函数内部的实际操作应该是无关紧要的)?
我的意思是......将内联运算符声明为成员,而不是作为成员,对实际函数的速度有任何通用影响,无论如何?
我有许多不同的结构,目前遵循内联成员运算符样式......但我想将其修改为有效的C代码,而不是; 所以在我这样做之前,我想知道性能/编译是否会有任何变化.
我在文档中读到了关于内联函数的内容.我的文档说:有两种类型的内联函数:implicity function和explicity function.
Explicity function:你inline在函数之前使用关键字,并在类之外使用.例如:
inline int Math::add(int a, int b){ return a + b; }
Run Code Online (Sandbox Code Playgroud)
Implicity function:类中的每个方法都是隐含的.例如:
class Math {
int add(int a, int b) { return a + b;} // implicity inline function
};
Run Code Online (Sandbox Code Playgroud)
所以,如果这是真的,那么,我不想使用的每一种方法,我都inline必须在课堂外宣布,对吧?如果这是真的,我可以在类中实现一个方法而不需要内联函数.
谢谢 :)
我一直想知道为什么我读过的大多数代码和教程总是在类中声明成员函数,然后像这样定义它.
class A{
A doSomething();
};
A A::doSomething(){
//doing something
}
Run Code Online (Sandbox Code Playgroud)
而不是这样做
class A{
A doSomething(){
//doing something
}
};
Run Code Online (Sandbox Code Playgroud)
无论如何,我正在修改我的教授的课程,我发现了这个:
" Toute fonctionmembredéfiniedanssa classe(danslaséclarationdela classe)estconsiéréeparle compilateur comme une fonction inline.Lemotcléinlinen'estplusutilisé. "
转化为;
" 在其类中定义的所有成员函数(在类'声明中)被编译器视为内联函数.不再使用关键字'inline' "
我理解的是,邻接内联函数就像宏一样工作.编译器将整个代码块复制到调用该函数的每个实例中.
我的问题是; 我教授的课程中的陈述是否正确,如果是,那背后的原因是什么?
现在我有2个c ++源文件:test9.cpp test10.cpp,它们都有一个同名的内联函数.
test9.cpp:
1 #include <iostream>
2 using namespace std;
3 void test();
4 inline void f1()
5 {
6 cout << "inline f1 in test9.cpp" << endl;
7 }
8 int main()
9 {
10 f1();
11 test();
12 return 0;
13 }
Run Code Online (Sandbox Code Playgroud)
test10.cpp:
1 #include <iostream>
2 using namespace std;
3 inline void f1()
4 {
5 cout << "inline f1 in test10.cpp" << endl;
6 }
7 void test()
8 {
9 f1();
10 }
Run Code Online (Sandbox Code Playgroud)
现在用g …
在Agner Fog的优化C++手册中,他有一节"内联函数有一个非内联副本",他写道
函数内联具有复杂性,即可以从另一个模块调用相同的函数.编译器必须制作内联函数的非内联副本,以便可以从另一个模块调用该函数.如果没有其他模块调用该函数,则此非内联副本为死代码.代码碎片化使得缓存效率降低.
让我们为此做一个测试.
foo.h中
inline double foo(double x) {
return x;
}
Run Code Online (Sandbox Code Playgroud)
t1.cpp
#include "foo.h"
double t1(double x) {
return foo(x);
}
Run Code Online (Sandbox Code Playgroud)
main.cpp中
#include <stdio.h>
extern double foo(double);
int main(void) {
printf("%f\n", foo(3.14159));
}
Run Code Online (Sandbox Code Playgroud)
编译,g++ t1.cpp main.cpp并正确运行.如果我这样做g++ -S t1.cpp main.cpp并查看程序集,我看到它main.s调用了一个定义的函数t1.s.这样做g++ -c main.cpp并g++ t1.cpp看着这些符号nm显示U _Z3food在main.o和W _Z3food中t1.o.所以很明显,Agner声称存在非内联副本是正确的.
怎么样g++ -O1 t1.cpp main.cpp?由于foo未定义,因此无法编译. 做的 …
我正在构建一个模板化的标头C++ 11库.正如我所读到的,我应该定义我的成员函数,inline以保证不会发生翻译单元中多重定义的定义.
这是inline关键字的明确定义吗?我曾经认为这inline是与性能相关的事情.我怀疑通过使用inline就像打开一个后门一样.使用它的任何缺点?还有其他方法吗?
当谈到CUDA时,我完全是"新手".所以,如果我的问题是微不足道的,请原谅我.
nvcc是否理解inlineC关键字的含义?
我知道__forceinline__,和类似的nvcc"宏",因此我不是在问如何编写inlinecuda设备端代码.
我也知道,我的代码在nvcc和c/c ++编译器之间"分开"(我正在使用Visual Studio IDE).
这是否意味着inline当nvcc"紧挨着" __device__或__global__内核时,nvcc会忽略该关键字?
编辑:
PS我曾搜索过cuda编程指南.我在inline条目下找不到任何有用的东西,类似的"标签"也无济于事.
我刚刚完成了Bruce Eckel的内联函数章节:用C++思考.那么有一个练习要求你创建两个精确的函数,一个是内联函数,另一个函数不是.然后使用clock()并计算每个传递的时间.我已经处理过类似的问题,我认为它没有任何复杂性.因此我想出了:
#include <iostream>
#include <ctime>
using namespace std;
inline int infun(int x) {
x = 3;
x = 5;
cout << "";
return x;
}
int fun(int x) {
x = 3;
x = 5;
cout << "";
return x;
}
int main() {
clock_t startIn = clock();
for (int i = 0; i < 10000000; i++) {
infun(i);
}
clock_t finishIn = clock();
clock_t start = clock();
for (int i = 0; i < 10000000; i++) {
fun(i); …Run Code Online (Sandbox Code Playgroud) 我的问题是扩展这个:为什么lambdas可以通过编译器比普通函数更好地优化?
重申一下,结论是lambdas创建了不同的特化,编译器可以简单地内联,而函数指针不像内联那么容易,因为对于一组函数原型只有一个特化.考虑到这一点,将函数指针模板作为快速/更快的lambdas?
int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }
template <class F>
int operate(int a, int b, F func)
{
return func(a, b);
}
template <int func(int, int)>
int operateFuncTemplate(int a, int b)
{
return func(a, b);
}
int main()
{
// hard to inline (can't determine statically if operate's f is add or sub since its just a function pointer)
auto addWithFuncP = …Run Code Online (Sandbox Code Playgroud)