Creating static library and linking to it with pre

2019-04-28 15:15发布

问题:

I am currently trying to learn how to use premake 4 in order to apply it to the 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.

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.h contains the prototype for the method print(). print() simply prints a line to the console. Using premake, I linked the static library to the Main project, and in main.cpp I have included the Test.h file. My problem is this: in VS2010 I get this error when I attempt to build:

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:

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:

#include <iostream>

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

Test.h:

void print();

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. 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.

回答1:

links {"Test"}

Lua is not Python. Whitespace is irrelevant to Lua, just like whitespace doesn't matter to C++. So your links statement only applies to the "Release" configuration. 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.

Premake4 works this way so that you could have certain libraries that are only used in a "Release" build (or Debug or whatever). Indeed, you can put almost any project command under a configuration. So you can have specific files that are used only in a debug build, or whatever.