# Problem with integrating lua in C++.

This topic is 3632 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Ok, I am new to using scripting languages in my applications, I am doing this for the first time. My problem right now is this, the debug version works and runs, I can edit the lua file and rerun the program without rebuilding, but when I try to build a release version(I'm using MSVS2008) I get like 6 linking errors. Here is my C++ code, Its a Win32 console application. -------------------------------------- extern "C" { #include<lua/lua.h> #include<lua/lualib.h> #include<lua/lauxlib.h> } #include<iostream> using std::cout; using std::endl; int main (int argc, char *argv[]) { int s=0; lua_State *L = lua_open(); //load the libs luaL_openlibs(L); //run a Lua scrip here luaL_dofile(L, "do-me.lua"); cout << "\nI am done with Lua in C++.\n" << endl; lua_close(L); system( "pause" ); return 0; } -------------------------------------- here is my lua code (file name do-me.lua) -------------------------------------- print "Start" for i=1,12 do print(i) end print "End" -------------------------------------- Like I said, it works like it's suppose to, I can edit the lua file and the rerun the program without rebuilding, but the only problem is the fact that when I build a release version it fails and gives me this: ----------------------------- ------ Build started: Project: new lua, Configuration: Release Win32 ------ Compiling... main.cpp Linking... main.obj : error LNK2001: unresolved external symbol _luaL_newstate main.obj : error LNK2001: unresolved external symbol _lua_pcall main.obj : error LNK2001: unresolved external symbol _luaL_loadfile main.obj : error LNK2001: unresolved external symbol _luaL_openlibs main.obj : error LNK2001: unresolved external symbol _lua_close C:\...\Visual Studio 2008\Projects\lua\new\Release\new lua.exe : fatal error LNK1120: 5 unresolved externals Build log was saved at "file://c:\...\Visual Studio 2008\Projects\lua\new\new\Release\BuildLog.htm" new lua - 6 error(s), 0 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== ------------------------------------ Please, if anyone could help me, I would appreciate it very much. Also, as I said before I'm a noob, but my debug exe. file works, but it won't show the lua code without the lua file in the same directory, I'm just assuming thats the way it's suppose to be, correct??? p.s. sry for the way I display the code, I don't know how to display it as code.

##### Share on other sites
There are [ source ] tags. Also, in your properties you need to also link to the lua library in your release configuration, not just the debug configuration.

##### Share on other sites
You're not linking the lua library. It's set up in debug mode, but not in release.

Look under linker settings. Alternatively, you can add entire lua as project, and don't use library at all.

##### Share on other sites
ok, now it works but now when the console lanches I get a runtime error that says Application as made an attempt to load the C runtime library incorrectly. Does anyone know whats wrong???

##### Share on other sites
Without more information its impossible to determine. Are you building Lua as a seperate project and loading it up dynamically at runtime as a dll? If so you'll need to build and link with Lua using the same runtime libraires as your applicaiton ie threaded vs multithreaded, dll vs static lib etc.. Look in your project properties for both projects and make sure they are using the exact same runtime enviroement.

It's easier just to include Lua as a static lib and avoid the whole issue of runtime configuration, that's what I do.

Good Luck!

-ddn

##### Share on other sites
Quote:
 Original post by ddn3It's easier just to include Lua as a static lib and avoid the whole issue of runtime configuration, that's what I do.

I would go a step further and say, just include the Lua source code as part of your project :) It's quite small and all your linking problems will go away.

I remember having issues trying to statically link the .lib downloaded from the website, and that's the solution that worked for me.

##### Share on other sites
Ok, I got that working now. But I do have one warning.

LINK : warning LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs; use /NODEFAULTLIB:library

Does anyone know what that is?? By the way, I got all my lua header files in the include folder, I got my lua51.lib and lua5.1.lib in the lib folder of the VC directory.

Also, do you usually have to move the script yourself to the Debug/Release folder??? Because when I go to test the EXE, the script is not there so it executes without the script, but of course once I place a copy of the lua script file into the directory, it works just fine.

##### Share on other sites
Quote:
 LINK : warning LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs; use /NODEFAULTLIB:library

http://msdn.microsoft.com/en-us/library/2kzt1wy3(VS.80).aspx
You've probably compiled your Lua library as a Debug version and your executable as a Release version, or the other way around. Anyway,
Quote:
 I would go a step further and say, just include the Lua source code as part of your project

That's the best solution. Especially when you aren't developing a console app, you'll want to redirect certain output to certain files/streams (different files for parse errors and io.write), and you'll want to interact with other parts of your project like a console or log window.

Quote:
 Also, do you usually have to move the script yourself to the Debug/Release folder???

It might be easier to set the output file to the same folder for Debug and Release and give the executables different names (for example, project.exe and projectD.exe in the same folder). Otherwise, when your project size increases your directory tree will become messy.

##### Share on other sites
Quote:
 Original post by beunIt might be easier to set the output file to the same folder for Debug and Release and give the executables different names (for example, project.exe and projectD.exe in the same folder).

I agree. This collects your project location and its resources into one convenient directory. (And when you're all done, you can zip that folder and ship it immediately [smile])

Here's how you can set it up do it automatically for any general project:

(This took me a lot of experimenting and time to get right, so I thought I might spare you the trouble)
1. Open up project properties.
2. In the left pane, select "Configuration properties"->"Linker"->"General"
3. Select the "Debug" configuration, in the Configuration combo box (alt+C).
4. Set "Output file" to be "$(SolutionDir)bin\$(ProjectName)_debug.exe"
5. Click "Apply"
6. Select the "Release" configuration.
7. Set "Output file" to be "$(SolutionDir)bin\$(ProjectName).exe"
8. Click "Apply"
9. Select the "All Configurations" configuration.
10. Select "Configuration properties"->"Debugging"
11. Set "Working directory" to "\$(TargetDir)"
12. Click "Apply"

[Edited by - _fastcall on August 11, 2008 4:25:45 PM]

1. 1
2. 2
3. 3
Rutin
21
4. 4
5. 5
gaxio
10

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631778
• Total Posts
3002310
×