直接编译源代码 [英] Compiling source code out of the blue

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

问题描述



我会在gcc新闻组上发布这个帖子,但是我会更有效率地说话。

到墙上。


无论如何,让我们说有人向你抛出一些源代码来获得一个

特定程序,并说,只需编译它,它就能正常工作。现在

诚然,我倾向于对这种情况感到厌倦,因为我记得

来自我的Windows时代我无数次被给予代码

应该是好的去,但是因为某些愚蠢的原因而无法编译。当然我喜欢编程,但是我无法通过查找DWORD的狡猾定义的头文件来讨论

这是隐藏在#if块中的
到Windows版本。


无论如何,假装我有信心收到一个程序的'b $ b b源代码来编译产生可执行的二进制文件,我只想询问如何最好地编译它在发布模式下。使用gcc。我不需要

错误或警告,我只想要可执行文件。


目前,我正在解压缩zip文件,打开命令提示符

在相关目录中并执行以下操作:


gcc * .c -D NDEBUG -o prog.exe


我正在寻找执行以下操作的gcc命令行序列:


*编译并链接存在于其中的所有源文件(* .c)当前

目录。

*适用它想要的任何和所有优化。

*不给我警告(或任何输出问题)

*从可执行文件中删除所有垃圾(HelloWorld不应该是
400 KB)


我现在正在使用的方法可以解决这个问题,但是仍然可以执行文件有点大(对于一个足够简单的程序,大约需要25 KB)。

另外,我想知道我正在获得所有关于

o的优化ffer。


我最近进入跨平台编程,为Linux编译了一分钟和Windows另一个,这就是为什么我' '$

一直想知道什么是最好的Give Me An Executable使用方法

gcc。


只是出于好奇,gcc主要限制在正常的8位

9位可能在一个符号大小的int内填充?


-
$ b $bTomásóhéilidhe

解决方案

2008年1月1日星期二22:47:45 -0600,Tomásóhéilidhe写道

(文章< Xn *************************** @ 194.125.133.14>):


无论如何,让我们说有人向你抛出一些源代码来获得一个

特定程序,并说:只需编译它,它就可以了工作得很好。现在

诚然,我倾向于对这种情况感到厌倦,因为我记得

来自我的Windows时代我无数次被给予代码

应该是好的去,但是因为某些愚蠢的原因而无法编译。当然我喜欢编程,但是我无法通过查找DWORD的狡猾定义的头文件来讨论

这是隐藏在#if块中的
到Windows版本。



来自< any platform>的代码可能会发生这种情况。这不是很有趣,

,因为一个简单的声明,例如它只是工作正常,当从

代码中获取时尚未被移植,暗示很多关于这个人给你的是b $ b,几乎没有关于代码本身的内容。


无论如何,假装我有信心收到一个程序'

源代码进行编译以产生可执行二进制文件的时刻,我只想询问如何以最好的方式编译它发布模式使用gcc。我不需要

错误或警告,我只想要可执行文件。



您不需要错误或警告吗?你怎么可能支持呢,

/特别/前者?


目前,我正在解压缩zip文件,在相关目录中打开命令提示符

并执行以下操作:


gcc * .c -D NDEBUG -o prog.exe


我正在寻找执行以下操作的gcc命令行序列:


*编译并链接存在的所有源文件(* .c)目前的

目录。



Makefiles对于任何小型项目都很好。

如果您正在获得使用Visual ,项目文件可能对其他

平台没什么帮助,所以你可能不得不自己构建一些,除非你

非常喜欢逐个文件地手动调用东西。


*应用它想要的任何和所有优化。



您希望编译器决定优化设置吗?我认为

的好起点可能是-O2。


*不给我警告(或任何输出就此而言)



我认为这是一个非常糟糕的主意。通常你想要你能获得多少b $ b,特别是在初始端口。稍后您可能会认为

有些不会被解决,但是很多破损的代码将会无错误地编译,但是会有一些有意义的警告然后仍会生成

a二进制。


*从可执行文件中删除所有垃圾(HelloWorld不应该是

400 KB)



许多平台都有办法在链接后执行此操作,例如,尝试在
UNIX系统上使用man strip。 />


我现在使用的方法可以解决这个问题,但仍然可以使用

可执行文件(大约25 KB)一个简单的程序)。

另外,我想知道我正在获得所有关于

报价的优化。



gcc编译器有各种各样的优化设置,而打开和关闭的
通常需要更多的考虑而不仅仅是转到

他们都打开了。


我最近进入跨平台编程,编译

一分钟用于Linux而另一部分用于Windows,这就是为什么我已经想知道什么是最好的给我一个可执行文件"使用方法

gcc。



我能理解你想要应用KISS原则来减少其中的一部分,但我怀疑你会导致比你更多的问题

通过追求这条路来解决。


出于好奇,gcc主要限制在正常的8位

字节系统,或者它是否有各种不同系统的二进制文件,

9位可能在符号大小的int内填充?

http://gcc.gnu.org/ install / specific.html


上面介绍了其中一些,但并非全部。它应该是一个好的

起点。谷歌应该为你可能想到的特定的

处理器找到答案。


-

兰迪霍华德(2个删除FOOBAR)

精确观察的力量被那些没有得到它的人称为玩世不恭。 - George Bernard Shaw


Randy Howard< ra ********* @ FOOverizonBAR.netwrote in comp.lang.c:


来自< any platform>的代码可能会发生这种情况。这不是很有趣,

,因为一个简单的声明,例如它只是工作正常,当从

代码中获取时尚未被移植,暗示关于这个人的很多信息

给你的,而且几乎没有任何代码本身。



我倾向于处理命令行程序,它应该是完全可靠的。
便携式。例如,一个程序从MAC地址计算网卡的序列号

数。


你不要不需要错误或警告?你怎么可能支持,

/特别/前者?



因为该程序知道可以正常工作。在开发我自己的

代码时,我当然会使用高警告设置...但是当我给出了

源代码时我更喜欢接收二进制文件,我只想编译

到可执行文件并完成它。


使用Linux,人们会分发很多源代码。这么多,

gcc内置于操作系统中。您认为您正在为一个程序下载

a二进制文件,然后当您打开readme.txt时,它会告诉您



make

make install


Makefiles对于任何小型项目都很好。

如果您从MS获得使用Visual

等项目构建的项目,项目文件可能对其他

平台没有太大帮助,所以你可能不得不自己构建其中一些,除非你确实喜欢逐个文件地手动调用东西。



什么是makefile?它是传递给编译器的参数列表吗?

有没有标准类型的makefile,或者所有编译器都有不同的

格式?

< blockquote class =post_quotes>
> *应用它想要的任何和所有优化。



您希望编译器决定优化设置吗?我认为

一个好的起点可能是-O2。



有趣的是,我用-O3程序编译,现在二进制

无法正常工作...我'我会查看原因。


> *不会给我警告(或任何输出) )



我认为这是一个非常糟糕的主意。通常你想要你能获得多少b $ b,特别是在初始端口。稍后您可能会认为

有些不会被解决,但是很多破损的代码将会无错误地编译,但是会有一些有意义的警告然后仍会生成

a二进制。



正如我所说的,我更喜欢二进制文件,但我留下源代码,

所以我只想编译然后假装给我一个二进制文件来开始

with。


> *从可执行文件中删除所有垃圾(HelloWorld不应该是400 KB)



许多平台在链接后都有办法做到这一点,试试男人吧

UNIX系统,例如。



我看过strip随gcc而来。我对

感到好奇,这就是为什么需要这样做的原因?为什么用

垃圾填写可执行文件,它不需要? (假设我们正在发布模式中编译

课程)


-
$ b $bTomásóhéilidhe


在文章< Xn *************************** @ 194.125.133.14>中,
$ b $bTomásóhéilidhe< to*@lavabit.comwrote:

....


>我已经看过剥离了。随gcc而来。但是我对
感到好奇,这就是为什么需要这样做的原因?为什么用不需要的垃圾填写可执行文件呢? (假设我们正在发布模式中编译
当然)



编译(或者更准确地说 - 在这个过程中,你总是要

精确! - 链接)与-s。这将条带作为链接的一部分。


另请注意,大多数Linux-y情况下的默认操作是使用" -g"编译
;,将所有废话放入在那里的第一个

的地方。但是与-s链接将它删除。


注意:现在任何时候,有人会发布一个关闭主题,不能

在这里讨论,等等,等等,等等消息,告诉你makefiles

(以及你感兴趣的一切)是禁止的。



I''d post this on a gcc newsgroup but I''d be more productive talking
to the wall.

Anyway, let''s say someone throws some source code at you for a
particular program and says, "Just compile it, it works fine". Now
admittedly, I tend to have a phobia of this situation because I recall
from my Windows days the numerous times I was given code that was
supposedly "good to go", but which failed to compile for some stupid
reason. Of course I like to program, but I couldn''t be bothered going
through header files looking for the dodgy definition of DWORD which is
hidden within #if blocks pertaining to the Windows version.

Anyway, pretending I have faith for a moment in receiving a program''s
source code to compile to yield an executable binary, I''d just like to
ask how best to compile it "in release mode" using gcc. I don''t need
errors or warnings, I just want the executable.

At the moment, I''m unzipping the zip file, opening a command prompt
in the relevant directory and doing the following:

gcc *.c -D NDEBUG -o prog.exe

I''m looking for a gcc command line sequence that does the following:

* Compiles and links all the source files (*.c) present in the current
directory.
* Applies any and all optimisations it wants.
* Doesn''t give me warnings (or any output for that matter)
* Strips all the garbage out of the executable (HelloWorld shouldn''t be
400 KB)

The method I''m using at the moment does the trick, but still the
executable file is a bit big (roughly 25 KB for a simple-enough program).
Also, I''d like to know that I''m getting all the optimisations that are on
offer.

I''m getting into cross-platform programming lately, compiling
something for Linux one minute and Windows the other, which is why I''ve
been wondering what''s the best "Give Me An Executable" method of using
gcc.

And just out of curiosity, is gcc restricted mainly to normal 8-Bit
byte system, or does it have binaries for all sorts of different systems,
9-Bit ones with padding inside a sign-magnitude int perhaps?

--
Tomás ó héilidhe

解决方案

On Tue, 1 Jan 2008 22:47:45 -0600, Tomás ó héilidhe wrote
(in article <Xn***************************@194.125.133.14>):

Anyway, let''s say someone throws some source code at you for a
particular program and says, "Just compile it, it works fine". Now
admittedly, I tend to have a phobia of this situation because I recall
from my Windows days the numerous times I was given code that was
supposedly "good to go", but which failed to compile for some stupid
reason. Of course I like to program, but I couldn''t be bothered going
through header files looking for the dodgy definition of DWORD which is
hidden within #if blocks pertaining to the Windows version.

That can happen with code coming from <any platform>. It isn''t fun,
because a blanket statement like "it just works fine", when taken from
code that hasn''t been ported already, implies a lot about the person
giving it to you, and almost nothing about the code itself.

Anyway, pretending I have faith for a moment in receiving a program''s
source code to compile to yield an executable binary, I''d just like to
ask how best to compile it "in release mode" using gcc. I don''t need
errors or warnings, I just want the executable.

You don''t need errors or warnings? How can you possibly support that,
/especially/ the former?

At the moment, I''m unzipping the zip file, opening a command prompt
in the relevant directory and doing the following:

gcc *.c -D NDEBUG -o prog.exe

I''m looking for a gcc command line sequence that does the following:

* Compiles and links all the source files (*.c) present in the current
directory.

Makefiles are nice for this for anything but trivially small projects.
If you are getting projects that were built with something like Visual
from MS, the project files are likely to not help you much on other
platforms, so you may have to build some of these yourself, unless you
really like invoking things manually file by file.

* Applies any and all optimisations it wants.

You want the compiler to decide the optimization settings? I''d think a
good starting point might be -O2.

* Doesn''t give me warnings (or any output for that matter)

I think this is a horribly bad idea. Usually you want as many as you
can get, especially in an initial port. Later on you may decide that
some aren''t going to be addressed, but a lot of broken code will
compile without error, but with meaningful warnings and still generate
a binary.

* Strips all the garbage out of the executable (HelloWorld shouldn''t be
400 KB)

Many platforms have a means to do this after linking, try man strip on
UNIX systems, for example.

The method I''m using at the moment does the trick, but still the
executable file is a bit big (roughly 25 KB for a simple-enough program).
Also, I''d like to know that I''m getting all the optimisations that are on
offer.

The gcc compiler has a wide variety of optimization settings, and which
to turn on and off usually require more consideration than just "turn
them all on".

I''m getting into cross-platform programming lately, compiling
something for Linux one minute and Windows the other, which is why I''ve
been wondering what''s the best "Give Me An Executable" method of using
gcc.

I can understand you wanting to apply the KISS principle to minimize
some of it, but I suspect you''re going to cause more problems than you
solve by pursuing this path.

And just out of curiosity, is gcc restricted mainly to normal 8-Bit
byte system, or does it have binaries for all sorts of different systems,
9-Bit ones with padding inside a sign-magnitude int perhaps?

http://gcc.gnu.org/install/specific.html

The above addresses some of them, but not all. It should be a good
starting point though. Google should cough up answers for a specific
processor you may have in mind.

--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw


Randy Howard <ra*********@FOOverizonBAR.netwrote in comp.lang.c:

That can happen with code coming from <any platform>. It isn''t fun,
because a blanket statement like "it just works fine", when taken from
code that hasn''t been ported already, implies a lot about the person
giving it to you, and almost nothing about the code itself.


I tend to be dealing with command-line programs which should be fully-
portable. For example, a program to calcuate a network card''s serial
number from its MAC address.

You don''t need errors or warnings? How can you possibly support that,
/especially/ the former?


Because the program is know to work perfectly. When developing my own
code, I of course make use of high warning settings... but when I''m given
source code where I''d prefer to receive a binary, I just want to compile
to an executable and be done with it.

Working with Linux, people distribute source code a lot. So much so, that
gcc comes built-in to the operating system. You think you''re downloading
a binary for a program, and then when you open readme.txt, it tells you
to do:

make
make install

Makefiles are nice for this for anything but trivially small projects.
If you are getting projects that were built with something like Visual
from MS, the project files are likely to not help you much on other
platforms, so you may have to build some of these yourself, unless you
really like invoking things manually file by file.


What''s a makefile? Is it a list of parameters to pass to the compiler? Is
there any standard kind of makefile, or do all compilers have a different
format?

>* Applies any and all optimisations it wants.


You want the compiler to decide the optimization settings? I''d think
a good starting point might be -O2.


Interestingly enough, I compiled by program with -O3 and now the binary
doesn''t work properly... I''ll look into why.

>* Doesn''t give me warnings (or any output for that matter)


I think this is a horribly bad idea. Usually you want as many as you
can get, especially in an initial port. Later on you may decide that
some aren''t going to be addressed, but a lot of broken code will
compile without error, but with meaningful warnings and still generate
a binary.


As I said, I would have preferred a binary but I''m left with source code,
so I just want to compile it and pretend I was given a binary to begin
with.

>* Strips all the garbage out of the executable (HelloWorld shouldn''t
be 400 KB)


Many platforms have a means to do this after linking, try man strip on
UNIX systems, for example.


I''ve had a look at "strip" that comes with gcc. What I''m curious about
though, is why this needs to be done at all? Why fill an executable with
crap that it doesn''t need? (assuming we''re compiling in Release Mode of
course)

--
Tomás ó héilidhe


In article <Xn***************************@194.125.133.14>,
Tomás ó héilidhe <to*@lavabit.comwrote:
....

>I''ve had a look at "strip" that comes with gcc. What I''m curious about
though, is why this needs to be done at all? Why fill an executable with
crap that it doesn''t need? (assuming we''re compiling in Release Mode of
course)

Compile (or, more precisely - and in this ng, you always gotta be
precise! - link) with "-s". This does the strip as part of the linking.

Note also that the default operation in most Linux-y situations is to
compile with "-g", which puts "all that crap" in there in the first
place. But linking with "-s" will remove it.

Note: Any minute now, someone is going to post an "off topic, can''t
discuss it here, blah, blah, blah" message, telling you that makefiles
(and everything else you''re interested in) is verboten.


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

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