Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 556
user1032369 Avatar asked Nov 25 '11 16:11

user1032369


People also ask

How do I link a static library?

Static libraries are created by copying all necessary library modules used in a program into the final executable image. The linker links static libraries as a last step in the compilation process. An executable is created by resolving external references, combining the library routines with program code.

What happens when you link a static library?

Static linking increases the file size of your program, and it may increase the code size in memory if other applications, or other copies of your application, are running on the system. This option forces the linker to place the library procedures your program references into the program's object file.

Do static libraries have dependencies?

So what is a Static library?? This is the most straight forward way of using a library as the final result is a simple executable with no dependencies.

What is in a static library?

In computer science, a static library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable.


1 Answers

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.

like image 95
Nicol Bolas Avatar answered Sep 19 '22 19:09

Nicol Bolas