9

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.

Bart
  • 19,692
  • 7
  • 68
  • 77
user1032369
  • 549
  • 3
  • 8
  • 22

1 Answers1

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

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • That was the problem, however now i get a problem when I try to run the build, a popup which says: Unable to start program: 'file_location\.\Test.lib' The specified file is in an unrecognized or unsupported binary format. Any help with this? PS. What I changed was move Links {"Test"} to right below Language. – user1032369 Nov 25 '11 at 18:25
  • @user1032369: That's because you're trying to run the *library*, not the executable. Change the current "Startup Project". – Nicol Bolas Nov 25 '11 at 18:33
  • So I can only run premake programs by clicking the executable, i cant just run it from VS? – user1032369 Nov 25 '11 at 18:36
  • 2
    @user1032369: Visual Studio has the concept of a "Startup Project". This is the project that will be executed when you attempt to execute something from Visual Studio. You can *change* what the current startup project is by right-clicking on it in the list of projects. This has nothing to do with Premake. – Nicol Bolas Nov 25 '11 at 18:41
  • Thanks, that was what I needed. Sorry, I have only used VS for about a month, and I do not know each little part of it. Thanks for the help. – user1032369 Nov 25 '11 at 18:47