OpenGL SDK 如何使用 Premake 创建新项目
我最近开始学习 OpenGL,以开始使用 C++ 制作一些图形应用程序。我已经安装了 OpenGL SDK,并且能够在其上正确构建项目。然而,在 OpenGL SDK 网站上,几乎没有关于如何使用 SDK 的元素(例如 freeglut 等)创建新项目的信息。我有 Premake 4.0,并且我知道我必须对lua 文件,但是我不了解 lua,也不知道如何使用 Lua 文件创建新项目。那么你能帮我一下吗?我使用的是 VS2010,我应该创建项目,然后用 premake 做一些事情吗?或者创建某种 lua 文件,然后使用 premake ?任何帮助都会很棒,因为我非常迷失,并且真的很想开始使用 OpenGL。我对此进行了很多尝试,例如从 sdk 复制 lua 文件,但没有成功。
I have recently begun the process of learning OpenGL to start making some Graphical applications using C++. I have installed the OpenGL SDK and I am able to build the projects properly on that. However, on the OpenGL SDK site there is little to no information what-so-ever on how to create new projects using the elements of the SDK, such as freeglut etc. I have Premake 4.0 and I understand I have to do something with the lua files, I do not know lua however and am not sure how to use the Lua files to create a new project. So could you help me out? Im using VS2010, should I create the project, then do something with premake? Or create some sort of lua file, then use premake on that? Any help would be wonderful because I am very lost, and would really like to get started with OpenGL. I have experimented a lot with this, such as copying the lua files from the sdk, but that came with no luck.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您不熟悉 Premake4,那么我强烈建议您直接使用 Visual Studio 项目。如果您遇到问题,请准确地修改您的问题,具体说明您所做的事情以及 Visual Studio 在尝试构建时向您提供的错误消息。您应该包括:
注意:如果您不知道其中任何一个是什么,那么您需要停下来并了解有关 C++ 项目如何工作的更多信息。您需要了解编译器如何处理包含路径、静态库、#defines 等。
如果您不熟悉 Premake4,并且仍想将 Premake4 与 SDK 一起使用,那么您首先必须成为熟悉 Premake4,无需 SDK。我可以给你一个完整的 premake4.lua 脚本,你只需插入它,更改几行,一切就会神奇地工作(如果你想要的话,你可以看看 SDK 的示例是如何构建的。具体来说,示例/premake4.lua)。但如果我这样做,你就不会学到任何东西。您只是复制并粘贴代码,而对其工作原理没有丝毫了解。
因此,我将告诉您应该采取哪些步骤来学习如何使用 Premake4。
第 1 步:Hello World,Premake 风格。您应该创建一个 .cpp 文件作为 Hello World 应用程序。它只有一个标准的 main 函数,可以将“Hello World”打印到控制台。这是最简单的部分。
最困难的部分是 Premake4 脚本。您将编写一个 Premake4 脚本来构建该项目,而不是直接创建 Visual Studio 项目。
Premake4 文档可以引导您完成制作第一个解决方案和项目的步骤。您当然需要将文件添加到该项目。您还应该了解如何使用配置,以便您可以进行调试和发布构建。调试版本应该有符号,而发布版本应该进行优化。
第 2 步:多个项目。这里,您有两个 .cpp 文件:
test.cpp
和main.cpp
。在test.cpp
中,放置一个打印某些内容的函数。该函数不应该接受参数或任何东西。在main.cpp
中,您应该有main
函数来调用test.cpp
中定义的函数。还应该有一个test.h
,它具有test.cpp
中定义的函数的原型。这里的技巧是你没有将它们编译成同一个可执行文件。不直接。您需要两个项目:一个名为
test
,另一个名为main
。test
项目应该是一个静态库,它编译test.cpp
。main
项目将是实际的可执行文件,它编译main.cpp
。它们都应在其文件列表中包含test.h
。在这里,您将了解到解决方案可以有多个
项目
。这两个项目有不同的文件列表。每个项目都可以有一个单独的kind
,它单独确定该项目的构建类型。test
项目应该是StaticLib
,而main
项目应该是ConsoleApp
。您还需要学习使用 links 命令将它们链接在一起。
main
项目应使用links
来指定test
。test
不需要链接到某些东西。第3步:掌握目录。
在这里,您将执行与步骤 2 相同的操作。除了一件事:将
test.h
和test.cpp
放在不同的目录中(当前的)。您还需要在该目录中有一个test.lua
文件,您将使用dofile
命令从主premake4.lua
文件执行该文件。test.lua
是您定义test
项目的位置。使用solution
命令创建解决方案后,您可以随时对test.lua
文件调用dofile
。请注意,
main
项目需要更改它找到test.h
的目录。您还需要在main
项目中使用includedirs
命令告诉编译器在哪里搜索您包含的test.h
标头main.cpp
。第4步:回到SDK。现在,您应该已经足够熟悉 Premake4 了,可以回顾一下我向您指出的说明并更好地理解它们。然后,按照说明进行操作即可。当它告诉您脚本的第一行应该是什么时,请将其作为脚本的第一行。将
UseLibs
函数放在指定的位置;它甚至给你一个例子来说明它的去向。将UseLibs
视为links
和includedirs
的奇特组合。If you are not familiar with Premake4, then I strongly advise you to just use Visual Studio projects directly. If you're having trouble with that, then please amend your question with exactly what you did, and exactly the error messages that Visual Studio gave you when attempting to build. You should include:
Note: If you don't know what any of these are, then you need to stop and learn a lot more about how C++ projects work. You need to understand how compilers deal with include paths, static libraries, #defines, etc.
If you are not familiar with Premake4, and you still want to use Premake4 with the SDK, then you first must become familiar with Premake4 without the SDK. I could give you an entire premake4.lua script that you could just plug in, change a few lines, and everything would magically work (and if you want that, you could look at how the SDK's examples are built. Specifically
examples/premake4.lua
). But if I did that, you wouldn't learn anything. You'd just be copy-and-pasting code, without having the slightest understanding of how it works.So instead, I'm going to tell you what steps you should take to learn how to use Premake4.
Step 1: Hello World, Premake-style. You should make a single .cpp file that is a Hello World application. It just has a standard
main
function that prints "Hello World" to the console. That's the easy part.The hard part is the Premake4 script. Rather than creating a Visual Studio project directly, you are going to write a Premake4 script to build that project for you.
The Premake4 documentation can walk you through the steps of making your first solution and project. You will of course need to add files to that project. You should also learn how to use configurations, so that you can have a Debug and Release build. The Debug build should have symbols, and the Release build should be optimized.
Step 2: Multiple projects. Here, you have two .cpp files:
test.cpp
andmain.cpp
. Intest.cpp
, place a function that prints something. The function shouldn't take parameters or anything. Inmain.cpp
, you should have themain
function that calls the function defined intest.cpp
. There should also be atest.h
which has a prototype for the function defined intest.cpp
.The trick here is that you aren't compiling them into the same executable. Not directly. You want two projects: one named
test
and one namedmain
. Thetest
project should be a static library, which compilestest.cpp
. Themain
project will be the actual executable, which compilesmain.cpp
. Both of them should includetest.h
in their file lists.Here, you're learning that solutions can have multiple
project
s. The two projects have different file lists. Each project can have a separatekind
, which determines the type of build for that project alone. Thetest
project should be aStaticLib
, while themain
project should be aConsoleApp
.You will also need to learn to use the links command to link them together. The
main
project should uselinks
to specifytest
.test
does not need to link to something.Step 3: Mastering directories.
Here, you're going to do the same thing as Step 2. Except for one thing: put
test.h
andtest.cpp
in a different directory (a subdirectory of the current one). You also want atest.lua
file in that directory, which you will execute from your mainpremake4.lua
file with adofile
command. Thetest.lua
is where you define yourtest
project. You can calldofile
on thetest.lua
file anytime after you have created the solution with thesolution
command.Note that the
main
project will need to change the directory where it findstest.h
. You will also need to use theincludedirs
command in themain
project to tell the compiler where to search for thetest.h
header you include inmain.cpp
.Step 4: Back to the SDK. At this point, you should now be familiar enough with Premake4 to look back at the instructions I pointed you to and understand them a bit better. Then, just do what the instructions say. When it tells you what the first line of your script should be, make that the first line of your script. Put the
UseLibs
function where it says to put them; it even gives you an example of where it goes. Think ofUseLibs
as a fancy combination oflinks
andincludedirs
.