简体   繁体   English

从Perl脚本执行C ++程序;它需要编译器吗?

[英]Execute C++ program from Perl script; does it require a compiler?

I have a somewhat more complicated question this time. 这次我有一个更复杂的问题。 I have written a short Perl program where my final ouput is in an array. 我写了一个简短的Perl程序,我的最终输出是在一个数组中。 I want to execute a C++ script with a .hpp file extension within my Perl script on each element of my output (I have downloaded the source code from the UCSC Genome Browser). 我想在我的输出的每个元素上的Perl脚本中执行带有.hpp文件扩展名的C ++脚本(我已经从UCSC Genome Browser下载了源代码)。 This is really difficult for me since I'm a beginner in programming overall and don't know anything about C++. 这对我来说真的很难,因为我是整体编程的初学者,对C ++一无所知。 I've done some reading and I think the best for me would be to use a qx call. 我做了一些阅读,我认为对我来说最好的是使用qx通话。 I've tried that and it just opens the .hpp file in the text editor when I run my Perl script. 我试过了,它只是在我运行Perl脚本时在文本编辑器中打开.hpp文件。 I'm not sure if this is because I've erroneoulsy assigned the .hpp file extension to be opened with Editor or whether I need a compiler to run .hpp files in general. 我不确定这是不是因为我错误地将.hpp文件扩展名指定为使用Editor打开,或者我是否需要编译器来运行.hpp文件。

my $info = qx(primercheck.hpp);
print "primercheck.hpp is: $info\n";

An .hpp file is a header file, that generally contains just macros and declarations, not executable code. .hpp文件是头文件,通常只包含宏和声明,而不是可执行代码。 It is used to specify the interface to code in .cpp files and cannot be compiled on its own. 它用于指定.cpp文件中的代码接口,不能自行编译。

A typical C++ program consists of several .cpp and .hpp files that must be compiled in combination and then linked with each other and with library files to create an executable image. 典型的C ++程序由几个.cpp.hpp文件组成,这些文件必须组合编译,然后相互链接并与库文件链接以创建可执行映像。

So yes, you do need a compiler, and a lot more besides! 所以是的,你确实需要一个编译器,还有更多!

Assuming you are on *nix, you could use the gcc tool to first compile the source code at runtime, via a system call from your perl script, and then make another system call to run the generated executable file. 假设你在* nix上,你可以使用gcc工具在运行时首先通过perl脚本的system调用编译源代码,然后再进行一次系统调用来运行生成的可执行文件。

Also, further to Borodin's answer, even if your file with the hpp extension has executable code, it may get compiled but will not execute. 此外,除了Borodin的答案之外,即使您的hpp扩展名文件具有可执行代码,它也可能被编译但不会执行。 A workaround would be to make a temporary copy of the source and give it a cpp extension and compile it, to get an executable which you can call from your perl script. 解决方法是创建源的临时副本并为其提供cpp扩展并对其进行编译,以获取可从perl脚本调用的可执行文件。 However, this requires your hpp file to actually have executable code if you want any output. 但是,如果您想要任何输出,这需要您的hpp文件实际上具有可执行代码。 Here's an example, 这是一个例子,

# make a temp copy with cpp extension
system("cp primercheck.hpp primercheck.cpp");

# generate executable using gcc
system("gcc -o primercheck primercheck.cpp");

# set execute permissions on resulting file
system("chmod +x primercheck");

# call the executable
system("./primercheck");

# remove the temp cpp file
system("rm primercheck.cpp");

# optional - remove the executable file as well
system("rm primercheck");

Hope this helps. 希望这可以帮助。

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

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