Can't open a C dll from Lua - windows

I created a very simple C dll and I'm trying to load it using Lua interactive mode
When I use require I get:
error loading module 'LuaExperiment' from file 'C:\Lua\5.2.4\LuaExperiment.dll' The specified procedure could not be found
When I use package.loadlib I get
multiple Lua VMs detected
Bellow is the contents of my Lua installation folder (LuaExperiment is my project):
I did the following to create the folder above:
Opened a Developer Command Prompt for VS2015 as an Administrator
and entered the following commands:
> vcvars32
> cl /MD /O2 /c /DLUA_BUILD_AS_DLL *.c
> ren lua.obj lua.o
> ren luac.obj luac.o
> link /DLL /IMPLIB:lua5.2.4.lib /OUT:lua5.2.4.dll *.obj
> link /OUT:lua.exe lua.o lua5.2.4.lib
> lib /OUT:lua5.2.4-static.lib *.obj
> link /OUT:luac.exe luac.o lua5.2.4-static.lib
These are the changes I made to Project Properties:
Added C:\Lua\5.2.4; to VC++ Directories->Include Directories
Changed C\C++->Advanced->Compile As to Compile as C Code (/TC)
Added C:\Lua\5.2.4\*.lib; to Linker->Additional Dependencies
Here are the .h and .c files:
https://github.com/ImperfectVoid/C

Related

How do I specify additional directories for a linker in a cl compiler

I use the cl /EHsc fileName.cpp command in powershell to compile cpp files, and the thing is I cannot find on the web how to specify the paths to additional dependencies for a linker to know where to look for lib files. Same goes with headers
It's right there in the documentation.
cl /I c:\myheaders /I ..\onemore mycode.c /link /LIBPATH:.\foo /LIBPATH:c:\bar

GCC won't detect header files in same directory

I am trying to compile a bunch of c files but for some reason compiler won't detect the header files which are located in the same exact folder. Here is the error I am getting
C:\Users\bz733p\Desktop\EBI20\source\application.c:45:25: fatal error: application.h: No such file or directory
compilation terminated.
Batch file:
set COMP_DIR=C:\Users\xxxx\Desktop\EBI20\source
set BIN_DIR=C:\Apps\opentoolsuite\runtime\msys\gcc\4.5.1-tdm-x32\bin
set CFLAGS=-g -Wall -m32
:: Clean
echo Cleaning directory...
::call clean.bat
:: Start the build
::COMPiling
echo COMPiling files....
for %%i in ("%COMP_DIR%\*.C")do %BIN_DIR%\i686-w64-mingw32-gcc-4.5.1.exe -c %CFLAGS% "%%~i" -o "%%~dpni.o" > results.txt
pause
I verified that both files are located in the same directory Files in directory. This happens for all header files in the directory. I looked into but could not find any solution

How to use arm-none-eabi with multiple files?

I'm trying to make a modified version of the FlySky FS-i6 firmware. After a bit of searching, I found someone who decompiled the source code here.
I'm trying to compile using arm-none-eabi, which I already have installed from the Arduino IDE.
Here's a batch file I made to compile the files, which are just a bunch of .c and .h files under several directories, but I have no clue what arguments I'm supposed to be giving the arm-none-eabi-gcc command...
#echo off
set EABI_PATH="%LocalAppData%\Arduino15\packages\arduino\tools\arm-none-eabi-gcc\4.8.3-2014q1\bin"
#RD /S /Q temp
mkdir temp
for /D %%d in (*) do (
echo ---- Compiling Directory: %%d
rem C Files:
for %%f in (%%d/*.c) do (
"%EABI_PATH%/arm-none-eabi-gcc" -c -mcpu=cortex-m0plus -g "%%d/%%f" -o "temp/%%~nf.o" -mthumb -g3
)
rem Header Files:
rem for %%f in (%%d/*.h) do (
rem "%EABI_PATH%/arm-none-eabi-gcc" -c -mcpu=cortex-m0plus -g "%%d/%%f" -o "temp/%%~nf.o" -mthumb -g3
rem )
)
I've searched everywhere, but I can't find any tutorials or anything about how to use the tool. All I want to do is compile the files to a firmware.hex file, you'd think it would just be a simple one-liner...

Generating an installer using CMake + CPack + Wix

Summary:
How to generate Wix installer with CMake/CPack?
Details:
I am trying to generate an installer out from a project that uses Wix, but apparently, cmake/cpack insist to use nsis, for example, from the repo https://github.com/ngladitz/cmake-wix-testsuite
I picked the first example "basic" folder with the following contents for CMakeLists.txt
add_executable(hw hw.cpp)
install(TARGETS hw DESTINATION bin)
install(FILES hw.cpp DESTINATION src)
set(CPACK_WIX_UPGRADE_GUID "F9AAAAE2-D6AF-4EA4-BF46-B3E265400CC7")
include(CPack)
After generating the cmake:
cd <basic-root-folder>
mkdir MY_BUILD
cd MY_BUILD
cmake ..
I tried to run the package.vcxproj generated with
msbuild package.vcxproj
and got the error
"C:\src\Samples\CPack\cmake-wix-testsuite-master\basic\MY_BUILD\PACKAGE.vcxproj
" (default target) (1) ->
(PostBuildEvent target) ->
EXEC : CPack error : Cannot find NSIS compiler makensis: likely it is not ins
talled, or not in your PATH [C:\src\Samples\CPack\cmake-wix-testsuite-master\ba
sic\MY_BUILD\PACKAGE.vcxproj]
So it looks like it insist in using the NSIS generator.
Simply add following line before include(CPack)
set(CPACK_GENERATOR WIX)
You can also do this:
cmake ..
cpack -G WIX
By default, CPack uses the NSIS Generator on Windows.
Ok, I found how
By specifying the CPack generator like
cmake -DCPACK_GENERATOR=WIX ..
Then running
msbuild package.vcxproj
Invokes wix like this extract from the log (and a bunch of .wixobj files)
PostBuildEvent:
setlocal
cd C:\src\Samples\CPack\cmake-wix-testsuite-master\basic\MY_BUILD.wix
if %errorlevel% neq 0 goto :cmEnd
C:
if %errorlevel% neq 0 goto :cmEnd
"C:\Program Files (x86)\CMake 3.1.3\bin\cpack.exe" -C Debug --config ./CPackC
onfig.cmake
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
:VCEnd
CPack: Create package using WIX
CPack: Install projects
CPack: - Install project: Project
CPack: Create package
CPack: - package: C:/src/Samples/CPack/cmake-wix-testsuite-master/basic/MY_BU
ILD.wix/Project-0.1.1-win32.msi generated.
FinalizeBuildStatus:
Deleting file "Win32\Debug\PACKAGE\PACKAGE.tlog\unsuccessfulbuild".
Touching "Win32\Debug\PACKAGE\PACKAGE.tlog\PACKAGE.lastbuildstate".
Done Building Project "C:\src\Samples\CPack\cmake-wix-testsuite-master\basic\MY
_BUILD.wix\PACKAGE.vcxproj" (default targets).
Build succeeded.
0 Warning(s)
0 Error(s)
Also the .wix source files are generated at C:\src\Samples\CPack\cmake-wix-testsuite-master\basic\MY_BUILD.wix\\_CPack_Packages\win32\WIX
I found out about this arg (CPACK_GENERATOR) by inspecting CPackConfig.cmake generated by the original cmake.

print.c is missing from lua 5.2, trying to build from source

I am trying to build Lua from source and when I look at http://www.lua.org/source/5.2/ I basically see what I got in the source file, and I don't see print.c.
In my build.bat (I am doing this on Windows 7) I have this:
cl /MD /O2 /W3 /c /DLUA_BUILD_AS_DLL *.c
del *.o
ren lua.obj lua.o
ren luac.obj luac.o
ren print.obj print.o
link /DLL /IMPLIB:lua5.2.lib /OUT:lua5.2.dll *.obj
link /OUT:lua.exe lua.o lua5.2.lib
lib /out:lua5.2-static.lib *.obj
link /OUT:luac.exe luac.o print.o lua5.2-static.lib
If I just remove all references to print.o will there be a problem with what I compile when I try to embed this in my game?
In lua 5.1, under compiler you will see print.c, so I wonder if I should not do 5.2.
http://www.lua.org/source/5.1/
UPDATE
So, what I did was change my build.bat and removed the print.obj, but I think the rename was useful in order to not include lua.obj and luac.obj in the .lib file, so I rename them and then rename them back.
cl /MD /O2 /W3 /c /DLUA_BUILD_AS_DLL *.c
del *.o
ren lua.obj lua.o
ren luac.obj luac.o
link /DLL /IMPLIB:lua5.2.lib /OUT:lua5.2.dll *.obj
link /OUT:lua.exe lua.o lua5.2.lib
lib /out:lua5.2-static.lib *.obj
ren lua.o lua.obj
ren luac.o luac.obj
link /OUT:luac.exe luac.obj lua5.2-static.lib
You don't need print.c in Lua 5.2, the only public function it defined is moved to luac.c. See PrintFunction() now found in luac.c.
Also, it is not a good idea to rename .obj to .o in Windows. Use whatever object file name is preferred by your tool chain.
Edit: I now see the purpose of the renaming, and while it does work, I still think it is a bad practice.
The trick is that the CL command in your batch file is compiling every .c file in your current directory with identical options. As it happens, that isn't a problem, but it isn't a conventional way of doing this sort of thing.
Then, because a few of those files aren't actually part of the DLL you want to build, you rename those so that you can fit the rest on the LINK command line as a single wild card *.obj.
If you are going to use a batch file to build Lua yourself, then it would be better to list the .obj files that make up the DLL and each application on each relevant link line rather than depend on a wild card. As it is, if you had a build error in the CL command, you still build a DLL with all the rest of the objects, or worse, possibly with an out of date object for the file that didn't compile.
Then you might or might not successfully link lua.exe and luac.exe even though there were errors.
The right answer here is to use a build tool of some form to compile and link. Microsoft should have provided you NMake, it might have provided you their internal tool (used to drive builds of the Windows kernel and device drivers, I know it comes with the DDK tools but I think it is also in the Platform SDK and may be buried in VS) named BUILD, and there is always the option of adding a more broadly used build system such as GNU Make, CMAKE, or countless others.
If you insist on using the renaming trick because it is more expedient than learning to do it the right way right now, then please at least add some testing for the successful completion of each compile and link step with IF ERRORLEVEL 1 GOTO :EOF commands to end the batch file after a failure, as shown below.
As I edited the batch file, I notice another potential issue. You built all the OBJ files with /DLUA_BUILD_AS_DLL, but then make a static library and link luac.exe from it. I don't know offhand what features of the source text are modified by that definition, but it probably isn't wise to make a static library from OBJ files compiled with it instead of a DLL.
cl /MD /O2 /W3 /c /DLUA_BUILD_AS_DLL *.c
if ERRORLEVEL 1 goto :EOF
del *.o
ren lua.obj lua.o
ren luac.obj luac.o
link /DLL /IMPLIB:lua5.2.lib /OUT:lua5.2.dll *.obj
if ERRORLEVEL 1 goto :EOF
link /OUT:lua.exe lua.o lua5.2.lib
if ERRORLEVEL 1 goto :EOF
cl /MD /O2 /W3 /c *.c
if ERRORLEVEL 1 goto :EOF
del *.o
ren lua.obj lua.o
ren luac.obj luac.o
lib /out:lua5.2-static.lib *.obj
if ERRORLEVEL 1 goto :EOF
ren lua.o lua.obj
ren luac.o luac.obj
link /OUT:luac.exe luac.obj lua5.2-static.lib
if ERRORLEVEL 1 goto :EOF

Resources