Creating static library and linking to it with premake - c++

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.

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.

Related

Keep on getting LNK2019 error when mixing C and C++

I'm trying to call C code from C++. I have 3 files: pubsub.cpp, pubsublib.c & pubsublib.h.
I know I have to use extern "C" in order to call a C function from C++.
I've literally tried everything, but I am still getting the following LNK2019 unresolved external symbol error:
LNK2019 unresolved external symbol _thisIsATestFunction referenced in
function _main
pubsublib.c
#include "pubsublib.h"
void thisIsATestFunction()
{
// do something
}
pubsublib.h
#ifndef pubsublib_H
#define pubsublib_H
void thisIsATestFunction();
#endif
pubsub.cpp
#include <string>
#include <iostream>
#ifdef __cplusplus
extern "C"{
#endif
#include "pubsublib.h"
#ifdef __cplusplus
}
#endif
using namespace std;
int main() {
thisIsATestFunction();
return 0;
}
I have also tried to just include the functions but I'm getting the same error. I'm really lost at the moment and have searched a lot... Any help would be greatly appreciated.
It works perfectly fine when you actually link with the necessary file.
When you compile pubsub.cpp you have to add pubsublib.obj to the command line. For example, this solved the problem for me using Visual Studio 2015:
cl -c pubsublib.c
cl pubsub.cpp pubsublib.obj
If you're building with the IDE, you simply need to make sure that pubsublib.c is a dependency and pubsublib.obj is specified as input to the link step.
You have to add a reference to the library in your application.
If you have both projects in one solution in Visual C++ 2015 you
right click your application project
select "Add..." from the drop down menu
select "Reference" from the menu
check you library in the list
If you have each project in its own solution you
right click you application project
select "Properties"
open "Linker" section "Input" page
add your .lib file to the "additional dependencies"
Your application should compile without LNK2019 after doing so.
There are also "build dependencies" to manage, but these do not affect the libraries linked to your project (only the build order).
(I am using a german edition of Visual Studio so please fix the translations if some of them are wrong)
Your declaration works fine, as proves the error message (_thisIsATestFunction is indeed a C-undecorated name).
Now two possibilities remain
the file pubsublib.c was not compiled as C, causing the compiled function to get decorated;
the file pubsublib.c was compiled as C, but didn't take part to the linking.
Only your solution/project(s) file can tell the truth. Can we see a screen copy of the Solution Explorer ?
This linkage error is produced when "pubsublib.c" file is compiled as C++ file (in g++). Please change your project options so that all .cpp files are compiled with g++, and all .c files are compiled with gcc.
After changing settings make sure that "pubsublib.c" file is really compiled with gcc. It can be found in compilation output like following:
g++.exe -c main.cpp -o main.o
gcc.exe -c pubsublib.c -o pubsublib.o
g++.exe main.o pubsublib.o -o Project1.exe

Including files from a seperate project in the same solution in Visual studio - LNK2001?

I had a solution named fun.sln with a project called fun.vcxproj.
I created a whole bunch of name spaces ready to be used.
I made another project called no_more_fun.vcxproj.
I added the includes directory for fun.vcxproj to the configuration of no_more_fun.vcxproj.
I added this to no_more_fun.cpp
#include "candy.h"
void main(void)
{
candy::get();
return;
}
candy.h is in the default directory for fun.vcxproj(which was added to the config)
But I get...
LNK2001 unresolved external symbol "int __cdecl candy::get(unsigned long)" (?get#candy##YAHK#Z) .....
Visual Studio shows no error before compiling.
The "candy" namespace works fine in the "fun" project so idn...
Is there a guide or something so that i can understand how i can go about sharing code efficiently among different projects within ONE solution?
This is a linker error. You didn't get any error at compile time, because the compiler found the candy::get() method in candy.h header, but the implementation (which I suppose is in a candy.cpp file) is not found by the linker.
Just add candy.cpp too to no_more_fun.vcxproj.
ps. I didn't observe but in the error message you can see that the function waits a parameter.
call it like this:
unsigned long foo = 0;
candy::get(foo);
This is going to sounds stupid but...i just dragged the files in to visual studio so that the no_more_fun project had the "files" in its "directory" too.
wow... I shouldn't need to do that...Am I wrong?(sarcasm).

Linker error with Lua and vs2015

I'm very new to using visual studio, and c++.
I was trying to build a game to test my OOP understandings using new language, then I realized I might need to start using scripting language with in my C++ game. I found LUA to be a good candidate for the scripting part of the game, so I decided to follow a tutorial I've found at :
http://www.gamedev.net/page/resources/_/technical/game-programming/the-lua-tutorial-r2999
#include "stdafx.h"
#include <lua.hpp>
#include<iostream>
int main()
{
char *Lua =
"x = 8 "
"return ( x > 7 ) ";
lua_State *luaState;
luaState = luaL_newstate();
int iStatus = luaL_loadstring(luaState, szLua);
if (iStatus)
{
std::cout << "Error: " << lua_tostring(luaState, -1);
return 1;
}
return 0;
}
However VS 2015 debugger is giving
unresolved external symbol _luaL_newstate
unresolved external symbol _luaL_loadstring
unresolved external symbol _lua_tolstring
I'm currently using Lua 5.1.5, and followed the tutorial setting tutorial section step by step, where it tells me to add lua folders to project settings.
Can someone tell me what i'm doing wrong?
The linker errors indicate that you are missing Lua functions from the executable you want to build. It's possible that you missed this step in the tutorial you've been following: add the Lua source files to your project's "Source Files". There is a list of the files in C:\dev\lua-5.1.5\etc\all.c; you want all of those files except for lua.c.
In general, you need to add the Lua library, lua DLL, or Lua files (in this case it's not enough to specify path to them), so that the references for the functions you are using in your code are properly resolved (statically or dynamically).

Qr read and generation under QT creator using leadtools

I want to buy license for QR codes read and generating from Leadtools but first I want to try their demo tools. I'm using MSVC 2013 x64 compiler. I think I did everything as follows in documentation:
Copied all dll's to my project directory (where build and release folder are located)
Copied Include and Lib folders to my project directory and add this lines to .pro file.
LIBS += -L$$PWD/Lib/CDLLVC12/x64/ -lLtkrn_x
INCLUDEPATH += $$PWD/Include
PRE_TARGETDEPS += $$PWD/Bin/CDLLVC12/x64/Ltkrnx.dll
include and #define LTV19_CONFIG, here is my code:
#define LTV19_CONFIG
#include <iostream>
#include <Ltkrn.h>
#include <ClassLib/LtWrappr.h>
using namespace std;
int main( ){
if( LT_KRN == LBase::LoadLibraries( LT_KRN, LT_DLGKRN))
cout << "success" << endl;
L_TCHAR licenseFile[] = L"d:\\temp\\TestLic.lic";
L_TCHAR key[] = L"xyz123abc";
LSettings::SetLicenseFile( licenseFile, key);
return 0;
}
Ask leadtools support, but they don't have much experience with working with QT...
When I tries to build application I get following errors:
LNK2019: unresolved external symbol "__declspec(dllimport) public: static unsigned int __cdecl LBase::LoadLibraries(unsigned int,unsigned int)" (__imp_?LoadLibraries#LBase##SAIII#Z) referenced in function main
LNK2019: unresolved external symbol "__declspec(dllimport) public: static int __cdecl LSettings::SetLicenseFile(wchar_t *,wchar_t *)" (__imp_?SetLicenseFile#LSettings##SAHPEA_W0#Z) referenced in function main
For following methods documentation says that I only need one dll/lib package (ltkrn). How to fix it? Still I don't get differences between static and dynamic linkage and this could be the problem.
If your linker accepted the 64-bit Ltkrn_x.lib, this suggests the problem is related to how you're using LEADTOOLS and not to QT. That's why I'm posting this as suggested reply instead of a note.
When programming using LEADTOOLS with C++, you normally use one of 2 sets of headers and LIBs:
Either include L_Bitmap.H (or a bunch of headers that includes LtKrn.H) and use the Ltkrn_x, Ltfil_x, etc. set of LIB files.
Or include ClassLib\LtWrappr.h and use only one LIB file, which in your case is Ltwvc_x.lib
Although in both cases you would be using many of the same DLL files such as Ltfilx.dll and Ltkrnx.dll, the reason you don't need their LIB files when using LtWrapper is that the ClassLibrary performs late (on demand) loading of these DLLs at run-time instead of referencing their LIB files at link time.
That's also why you need to call LBase::LoadLibraries() and specify the DLLs you need before your code uses these DLLs.
So to summarize, please try this:
Remove #include "Ltkrn.h"
Remove the linker reference to Ltkrn_x.lib (although you'll need the DLL)
Keep #include "ClassLib/LtWrappr.h"
Add a linker reference to Ltwvc_x.lib

Having trouble embedding Lua for Windows install into C++ program

This is the first question I have found myself not being able to get to the bottom of using my normal googling/stack overflowing/youtubing routine.
I am trying to compile a minimal Lua program inside of a C++ environment just to ensure my environment is ready to development. The Lua language will be later used for User Interface programming for my C++ game.
First some basic information on my environment:
Windows 7 64-bit
Visual studio 2010
Lua for Windows 5.1 (latest build I could download from google code)
Here is the code I am trying to compile:
// UserInt.cpp : Defines the entry point for the console application.
//
#pragma comment(lib,"lua5.1.dll")
#include "stdafx.h"
#ifndef __LUA_INC_H__
#define __LUA_INC_H__
extern "C"
{
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
int _tmain(int argc, _TCHAR* argv[])
{
lua_State * ls = luaL_newstate();
return 0;
}
#endif // __LUA_INC_H__
Here is the Error I am getting:
1>UserInt.obj : error LNK2019: unresolved external symbol _luaL_newstate referenced in function _wmain
1>c:\users\deank\documents\visual studio 2010\Projects\UserInt\Debug\UserInt.exe : fatal error LNK1120: 1 unresolved externals
Things I have tried:
I have read about lua_open()(and several other functions) no longer being used so I tried the newstate function instead. I get the same error. This was more of a sanity check than anything. I am using 5.1 and not 5.2 so I do not think this really matters.
I have also read this thread Cannot link a minimal Lua program but it does not seem to help me because I am not running the same environment as that OP. I am on a simple windows 7 and visual studio environment.
The top pragma comment line was something I saw in yet another thread. I get the same error with or without it.
I have gone into my visual studio C++ directories area and added the lua include to the includes and the lua lib to the libraries.
So it seems like my program is seeing the .h and seeing the symbol. But for some reason it is not getting the .cpp implementation for the functions. This is why I was hoping including that .dll directly would help fix the problem, but it hasn't.
So, I feel like I have exhausted all of my options solving this on my own. I hope someone is able to help me move forward here. Lua looks like an awesome language to script in and I would like to get my environment squared away for development.
I hope it is just some silly error on my part. I believe I have provided as much information as I can. If you need more specifics I will update with info if I can provide it.
Edit1
Tried the solution in this Can't build lua a project with lua in VS2010, library issue suspected
That did not work either.
You'll need to have the library (.LIB) file and add that to VS. Use Project > Properties and go to Linker > Input and add the full .lib filename to the "Additional Dependencies" line. Note that the .LIB is different from the .DLL.
Personally, I prefer adding the source code to my project, over referencing the dynamic link library. The following procedure will let you do as such.
Download the source code ( http://www.lua.org/ftp/ ), uncompress it.
In Visual Studio, choose File > New > Project and choose Visual C++, Win32, "Win32 Console Application".
In your project in Visual Studio, add all the source code, except luac.c. Also delete the main() function out of the file that VS created for you. This is usually given the name of the project you specified with the .cpp file extension. You could just remove this file all-together from the project.
Build and Run.
This is the Lua console