编译C ++ - 无文件的代码 [英] Compiling C++-code without a file

查看:95
本文介绍了编译C ++ - 无文件的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用标准的g ++编译器编译一些C ++代码。
然而,不是从文件编译:

I'm trying to compile some C++ code using your standard g++ compiler. However, rather than compiling from a file:

main.cpp:

#include <iostream>

int main(){
    std::cout << "Hello World!\n";
    return 0;
}

我喜欢做

g++ ... "#include <iostream>\n int main(){ std::cout << \"Hello World!\n\"; return 0;}"

前一个post 显示

echo "int main(){}" | gcc -Wall -o testbinary -xc++ -

有效,但我想知道它是如何工作的

works but I would like to know how it works and better yet, if there is a way to do this without the need to pipe the contents.

编辑:

我在做运行时代码生成,我需要生成一个共享库并加载创建的函数。

I'm doing run-time code generation where I need to generate a shared library and load the functions created.

我认为会有一个标志告诉编译器嘿,我给你的源代码,而不是文件。

I thought there would be a flag to tell the compiler "hey, I'm giving you the source code and not the file".

再次感谢帮助!

推荐答案


echoint main(){}| gcc -Wall -o testbinary -xc ++ -

echo "int main(){}" | gcc -Wall -o testbinary -xc++ -

工作,但我想知道它是如何工作,更好的,如果有一种方法,

works but I would like to know how it works and better yet, if there is a way to do this without the need to pipe the contents.

也可以说(例如在shell脚本中):

Alternatively you can say (e.g. in a shell-script):

gcc -Wall -o testbinary -xc++ - << EOF
int main(){}
EOF

这篇关于编译C ++ - 无文件的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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