如何从 C 编写的源文件构建可执行文件?

发布于 2024-10-04 12:28:21 字数 159 浏览 3 评论 0原文

src 目录下有少量以 .c 和 .h 为扩展名的文件(cmdline.c cmdline.h core.c core.h 等),还有一个没有扩展名的文件“MakeFile”。是否有可能将这些源文件构建成 Windows 7(64 位)上的某个可执行文件?我想我需要下载 C 编译器或一些 SDK,对吗?

There is few files with .c anf .h extensions (cmdline.c cmdline.h core.c core.h and so on) in src directory, also there is one file "MakeFile" without extension. Is there any possibility to build these source files into some executable file on Windows 7 (64bits) ? I think i need to download compilers for C or some sdks right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

画尸师 2024-10-11 12:28:21

是的。

你需要:

  • 下载并安装一个C/C++编译器(我推荐TDragon的MinGW发行版 ),
  • 将编译器添加到您的路径(大多数情况下安装程序可以为您完成);通过打开 cmd.exe 并输入 gcc -v 和 mingw32-make -v 来验证它是否已完成,两者都应该为您提供半屏的版本如果您的路径设置正确,
  • 请通过 cmd.exe 导航到 Makefile 所在的文件夹并调用 mingw32-make

从现在开始,一切都应该自动编译。如果没有,请发布错误。


更新:

首先,获取 MSys 包。安装它,您将拥有更新版本的 make(从现在开始使用它而不是 mingw32-make)。

关于CreateProcess bug,它与系统PATH变量太长有关。您需要执行以下操作:

  • 打开 cmd
  • 执行 set PATH=c:/mingw32/bin;c:/msys/1.0/bin (在此处更改路径以反映您自己的安装(如果不同),
  • 然后像以前一样:导航到项目的目录,运行 make。如果您没有丢失任何外部库,现在一切都应该很顺利。

顺便说一句 - 记住不要将 MinGW 或 MSys 安装在带有空格的目录中。

Yes.

You need to:

  • download and install a C/C++ compiler (I recommend TDragon's distribution of MinGW ),
  • add the compiler to your PATH (the installer can do it for you most of the cases); verify it's done by opening cmd.exe and typing gcc -v and mingw32-make -v, both should give you half a screenful of version information if your path is set correctly,
  • via cmd.exe, navigate to the folder in which the Makefile resides and call mingw32-make.

From now on everything should compile automatically. If it doesn't, post the errors.


Update:

First of all, it'd be useful for you to get the MSys package. Install it and you'll have a more recent version of make (use it instead of mingw32-make from now on).

About the CreateProcess bug, it has to do with the system PATH variable being too long. You'd need to do something like this:

  • open cmd
  • execute set PATH=c:/mingw32/bin;c:/msys/1.0/bin (change the paths here to reflect your own installation if it's different)
  • then as before: navigate to your project's directory, run make. Everything should be smooth now if you're not missing any external libraries.

BTW- remember not to install MinGW or MSys in directories with spaces.

╭⌒浅淡时光〆 2024-10-11 12:28:21

我不是 Windows 开发人员..
但据我所知还是这样。 Visual Studio(我猜是2008)有能力读取Makefile。
请看一下它。如果需要,请将此 makefile 更改为其格式。

有许多与平台无关的开源产品。它们使用它们提供的 Makefile 在两个操作系统上进行编译。

或者使用“cygwin”

开发人员C++在Windows中工作,但它实际上是购买到Windows中的GCC代码,有人熟悉他们用来将linux(.sh)转换为可执行文件的过程吗?

I am not a Windows Developer..
But still as per my knowledge. Visual Studio (i.e 2008, I guess) has the ability to read the Makefile.
Please have a look at it..and if needed change this makefile to their format..

There are many opensource product which are platform independent..and they get compiled on both OS with the just Makefile they provided.

Or else use 'cygwin'

Developer C++ works in windows but it is actually GCC code bought into Windows, Is anyone familiar about the procedure they used to convert the linux ( .sh) to executables ??

你げ笑在眉眼 2024-10-11 12:28:21

我想我需要下载 C 编译器或一些 SDK,对吧?

当然是编译器,但是您可能需要哪些额外的库完全取决于代码本身。成功的构建还可能取决于原始代码和 makefile 的预期目标。 makefile 可能是 GNU makefile,但还有其他类似但不兼容的 make 实用程序,例如 Borland Make 和 MS NMake。如果是简单的构建,您可以完全避免 makefile 问题并使用 IDE(例如 Visual C++ 2010 Express)提供的项目管理。

如果您不知道这段代码是什么、它的作用是什么以及它需要构建什么,那么您就完全在冒险构建它。也许您应该发布原始来源的链接,以便您可以获得有关如何构建它的更具体的建议。


[编辑]

好的,现在看看您尝试构建的代码,这是一个非常简单的构建,因此如果您想避免使用 GNU make,那么您可以添加所有 *.c将 src 文件夹中的文件复制到 IDE 中的项目并构建它。

然而,有一个严重的问题,它使用 BSD 套接字 API 和 Linux 系统标头。您需要首先将代码移植到 Windows API,例如 WinSock(与 BSD 套接字非常相似),或者在 Cygwin 下构建它(有点像大锤)。可能还有其他Linux依赖项需要排序,我没有详细查看,但看起来相当简单。也就是说,如果您没有关于编译这些东西的第一条线索,那么也许这不是您可以完成的任务?

当然,编译代码可能只是问题的一半,如果它被设计为在 Linux 上运行,则可能存在运行时依赖性阻止它在 Windows 上运行。我又没有仔细看过。

另外看看代码,我建议谨慎一些,这可能不是最好质量的代码。这可能不公平,但一个明显的缺陷以及缺乏经验的迹象是头球中缺乏后卫。

I think i need to download compilers for C or some sdks right?

A compiler certainly, but what additional libraries you may need will depend entirely on the code itself. A successful build may also depend on the intended target of the original code and makefile. The makefile may be a GNU makefile, but there are other similar but incompatible make utilities such as Borland Make and MS NMake. If it is a simple build, you may be able to avoid the makefile issue altogether and use the project management provided by an IDE such as Visual C++ 2010 Express.

If you do not know what this code is or what it does and what it needs to build, you are taking a risk building it at all. Maybe you should post a link to the original source so that you can get more specific advice on how to build it.


[EDIT]

Ok, now looking at the code you are attempting to build, it is a very simple build, so if you wanted to avoid using GNU make, then you could just add all the *.c files in the src folder to a project in your IDE and build it.

However there is one serious gotcha, it uses the BSD sockets API and Linux system headers. You will need to first port the code to Windows APIs such as WinSock (very similar to BSD Sockets), or build it under Cygwin (a sledgehammer for a nut somewhat). There may be other Linux dependencies that need sorting, I have not looked in detail, but it looks fairly simple. That said, if you did not have the first clue regarding compiling this stuff, then perhaps this is not a task you could do?

Of course compiling the code may only be half teh problem, if it was designed to run on Linux, there may be run-time dependencies that prevent it running on Windows. Again I have not looked in detail.

Also looking at the code, I would suggest some caution, this may not be the best quality code. That may be unfair, but one obvious flaw and an indication if inexperience is the lack of include guards in the headers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文