简体   繁体   English

创建静态库并使用premake链接到它

[英]Creating static library and linking to it with premake

I am currently trying to learn how to use premake 4 in order to apply it to the OpenGL sdk . 我目前正在尝试学习如何使用premake 4将其应用于OpenGL sdk I am currently trying to make a Visual Studio 2010 solution that constructs 2 projects, one being a static library, the other contains a single main source file, with the main method. 我目前正在尝试使用main方法构建一个构建2个项目的Visual Studio 2010解决方案,一个是静态库,另一个包含单个主源文件。

This project is extremely simple, and is solely for the purpose of learning premake. 这个项目非常简单,仅用于学习预制。 In the static library project, named Test, I have 2 files, Test.h and Test.cpp. 在名为Test的静态库项目中,我有2个文件,Test.h和Test.cpp。 Test.h contains the prototype for the method print(). Test.h包含方法print()的原型。 print() simply prints a line to the console. print()只是在控制台上打印一行。 Using premake, I linked the static library to the Main project, and in main.cpp I have included the Test.h file. 使用premake,我将静态库链接到Main项目,而在main.cpp中我已经包含了Test.h文件。 My problem is this: in VS2010 I get this error when I attempt to build: 我的问题是:在VS2010中,当我尝试构建时出现此错误:

1>main.obj : error LNK2019: unresolved external symbol "void __cdecl print(void)" (? print@@YAXXZ) referenced in function _main  
1>.\Main.exe : fatal error LNK1120: 1 unresolved externals

Here is my code in the 4 files, the premake4.lua: 这是我在4个文件中的代码,premake4.lua:

solution "HelloWorld"
    configurations {"Debug", "Release"}
project "Main"
    kind "ConsoleApp"
    language "C++"
    files{
        "main.cpp"

    }
    configuration "Debug"
        defines { "DEBUG" }
        flags { "Symbols" }

    configuration "Release"
        defines { "NDEBUG" }
        flags { "Optimize" } 
    links {"Test"}
project "Test"
    kind "StaticLib"
    language "C++"
    files{
        "test.h",
        "test.cpp"

    }

Test.cpp: TEST.CPP:

#include <iostream>

void print(){
    std::cout << "HELLO" << std::endl;
}

Test.h: Test.h:

void print();

Main.cpp: Main.cpp的:

#include <conio.h>
#include "test.h"
int main(){
    print();
    getch();
    return 0;
}   

If you are wondering why there is a getch() there, on my computer the console immediately closes once it reaches return 0, so I use getch() to combat that issue, which forces the window to wait until the user has pressed another key. 如果你想知道为什么有一个getch(),在我的计算机上,控制台一旦到达返回0就会立即关闭,所以我使用getch()来解决这个问题,这会迫使窗口等到用户按下另一个键。 Any advice on this issue would be wonderful, because I simply am not sure what the problem is. 关于这个问题的任何建议都很棒,因为我不确定问题是什么。 If it is something simple please dont castrate me on it, I have very little experience with premake and static libraries, which is why I am trying to learn them. 如果它是简单的请不要阉割我,我对premake和静态库的经验很少,这就是我试图学习它们的原因。

links {"Test"}

Lua is not Python. Lua不是Python。 Whitespace is irrelevant to Lua, just like whitespace doesn't matter to C++. 空白与Lua无关,就像空格对C ++无关紧要一样。 So your links statement only applies to the "Release" configuration. 因此,您的links语句仅适用于"Release"配置。 If you want it to apply to the project as a whole, it needs to go before the configuration statement, just like your kind , files , and other commands. 如果您希望它作为一个整体应用于项目,它需要在configuration语句之前,就像您的kindfiles和其他命令一样。

Premake4 works this way so that you could have certain libraries that are only used in a "Release" build (or Debug or whatever). Premake4以这种方式工作,因此您可以拥有某些仅用于"Release"构建(或Debug或其他)的库。 Indeed, you can put almost any project command under a configuration . 实际上,您几乎可以将任何 project命令置于configuration So you can have specific files that are used only in a debug build, or whatever. 因此,您可以拥有仅在调试版本中使用的特定文件,或者其他任何文件。

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

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