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

use*_*369 6 c++ static-libraries visual-studio-2010 premake

我目前正在尝试学习如何使用premake 4将其应用于OpenGL sdk.我目前正在尝试使用main方法构建一个构建2个项目的Visual Studio 2010解决方案,一个是静态库,另一个包含单个主源文件.

这个项目非常简单,仅用于学习预制.在名为Test的静态库项目中,我有2个文件,Test.h和Test.cpp.Test.h包含方法print()的原型.print()只是在控制台上打印一行.使用premake,我将静态库链接到Main项目,而在main.cpp中我已经包含了Test.h文件.我的问题是:在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

这是我在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"

    }
Run Code Online (Sandbox Code Playgroud)

TEST.CPP:

#include <iostream>

void print(){
    std::cout << "HELLO" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

Test.h:

void print();
Run Code Online (Sandbox Code Playgroud)

Main.cpp的:

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

如果你想知道为什么有一个getch(),在我的计算机上,控制台一旦到达返回0就会立即关闭,所以我使用getch()来解决这个问题,这会迫使窗口等到用户按下另一个键.关于这个问题的任何建议都很棒,因为我不确定问题是什么.如果它是简单的请不要阉割我,我对premake和静态库的经验很少,这就是我试图学习它们的原因.

Nic*_*las 11

links {"Test"}
Run Code Online (Sandbox Code Playgroud)

Lua不是Python.空白与Lua无关,就像空格对C++无关紧要一样.所以你的links陈述只适用于"Release"配置.如果你想让它适用于项目作为一个整体,它需要之前去configuration声明,就像你的kind,files以及其他命令.

Premake4以这种方式工作,因此您可以拥有某些仅在"Release"构建(或调试或其他)中使用的库.实际上,你几乎可以把任何 project命令放在一个configuration.因此,您可以拥有仅在调试版本中使用的特定文件,或者其他任何文件.

  • @ user1032369:Visual Studio具有"启动项目"的概念.这是当您尝试从Visual Studio执行某些操作时将执行的项目.您可以通过在项目列表中右键单击它来*更改当前启动项目的内容.这与Premake无关. (2认同)