C++ 如何在Linux中编译ISPC代码并将其与普通cpp文件链接?
我想编译一个ispc程序。我试图为他们的一个示例程序生成可执行文件 我有以下内容的simple.cppC++ 如何在Linux中编译ISPC代码并将其与普通cpp文件链接?,c++,ispc,C++,Ispc,我想编译一个ispc程序。我试图为他们的一个示例程序生成可执行文件 我有以下内容的simple.cpp #include <stdio.h> #include <stdlib.h> // Include the header file that the ispc compiler generates #include "simple_ispc.h" using namespace ispc; int main() { float vin[1
#include <stdio.h>
#include <stdlib.h>
// Include the header file that the ispc compiler generates
#include "simple_ispc.h"
using namespace ispc;
int main() {
float vin[16], vout[16];
// Initialize input buffer
for (int i = 0; i < 16; ++i)
vin[i] = (float)i;
// Call simple() function from simple.ispc file
simple(vin, vout, 16);
// Print results
for (int i = 0; i < 16; ++i)
printf("%d: simple(%f) = %f\n", i, vin[i], vout[i]);
return 0;
}
export void simple(uniform float vin[], uniform float vout[],
uniform int count) {
foreach (index = 0 ... count) {
// Load the appropriate input value for this program instance.
float v = vin[index];
// Do an arbitrary little computation, but at least make the
// computation dependent on the value being processed
if (v < 3.)
v = v * v;
else
v = sqrt(v);
// And write the result to the output array.
vout[index] = v;
}
}
#包括
#包括
//包括ispc编译器生成的头文件
#包括“简单的ispc.h”
使用名称空间ispc;
int main(){
浮动vin[16],vout[16];
//初始化输入缓冲区
对于(int i=0;i<16;++i)
vin[i]=(浮动)i;
//从simple.ispc文件调用simple()函数
简单(vin,vout,16);
//打印结果
对于(int i=0;i<16;++i)
printf(“%d:简单(%f)=%f\n”,i,vin[i],vout[i]);
返回0;
}
我有simple.ispc和以下内容
#include <stdio.h>
#include <stdlib.h>
// Include the header file that the ispc compiler generates
#include "simple_ispc.h"
using namespace ispc;
int main() {
float vin[16], vout[16];
// Initialize input buffer
for (int i = 0; i < 16; ++i)
vin[i] = (float)i;
// Call simple() function from simple.ispc file
simple(vin, vout, 16);
// Print results
for (int i = 0; i < 16; ++i)
printf("%d: simple(%f) = %f\n", i, vin[i], vout[i]);
return 0;
}
export void simple(uniform float vin[], uniform float vout[],
uniform int count) {
foreach (index = 0 ... count) {
// Load the appropriate input value for this program instance.
float v = vin[index];
// Do an arbitrary little computation, but at least make the
// computation dependent on the value being processed
if (v < 3.)
v = v * v;
else
v = sqrt(v);
// And write the result to the output array.
vout[index] = v;
}
}
export void simple(统一浮动vin[],统一浮动vout[],
统一整数计数){
foreach(索引=0…计数){
//为此程序实例加载适当的输入值。
浮动v=vin[索引];
//做一点随意的计算,但至少
//计算取决于正在处理的值
如果(v<3.)
v=v*v;
其他的
v=sqrt(v);
//并将结果写入输出数组。
vout[指数]=v;
}
}
我可以使用cmake获取可执行文件,但我想知道运行simple.cpp文件需要执行的原始命令。有人能告诉我如何使用ispc编译和运行simple.cpp文件吗?根据您的要求,您可以在终端中使用ispc
作为命令:
ispc simple.ispc-o simple.o
生成一个对象文件
simple\u ispc.h
:
-h标志还可用于指示ispc生成C/C++头
包含C可调用ispc函数的C/C++声明的文件
以及传递给它的类型
所以你可以做一些类似的事情
ispc simple.ispc-h simple\u ispc.h
然后
g++simple.cpp-o可执行文件
要获取可执行文件。首先,使用ispc编译器创建ispc头文件和目标文件
ispc --target=avx2-i32x8 simple.ispc -h simple_ispc.h -o simple_ispc.o
然后创建cpp的对象文件并将两个对象文件链接在一起
g++ -c simple.cpp -o simple.o
g++ simple.o simple_ispc.o -o executable
或者在创建ispc头文件和obj文件后,在一个命令中创建可执行文件
g++ simple.cpp simple_ispc.o -o executable
此外,如果您有clang/llvm,您也可以使用它们进行编译。以下是步骤:
simple.cpp文件包含
#包括“simple\u ispc.h”
。您可以发布命令从simple.cpp链接头文件获取可执行文件吗。我不知道怎么做。g++simple.cpp-o可执行文件/usr/bin/ld:/tmp/ccg6KMuX.o:在函数'main'中:simple.cpp:(.text+0x6a):对“simple”collect2的未定义引用:错误:ld返回1退出状态
我在运行第二个命令时出现以下错误。g++simple.cpp simple\u ispc.o-o可执行文件在使用ispc编译器创建obj和头文件后解决了此问题