Compiling Mongoose's hello.c under Windows - c

I am trying to compile the included hello.c example from Mongoose under Windows. I am using the Microsoft Visual command prompt and I have copied the mongoose.c and mongoose.h to the same directory as the hello.c example.
When I write "cl hello.c" I get the following output/error:
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
hello.c
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:hello.exe
hello.obj
hello.obj : error LNK2019: unresolved external symbol _mg_stop referenced in function _main
hello.obj : error LNK2019: unresolved external symbol _mg_start referenced in function _main
hello.obj : error LNK2019: unresolved external symbol _mg_printf referenced in function _begin_request_handler
hello.obj : error LNK2019: unresolved external symbol _snprintf referenced in function _begin_request_handler
hello.obj : error LNK2019: unresolved external symbol _mg_get_request_info referenced in function _begin_request_handler
hello.exe : fatal error LNK1120: 5 unresolved externals
There is a Makefile included with the examples and I have tried to use the Makefile to do the build, but do not understand how to do this. If i try "nmake hello.exe". I get the following output/error:
Microsoft (R) Program Maintenance Utility Version 10.00.30319.01
Copyright (C) Microsoft Corporation. All rights reserved.
cl -W -Wall -I.. -pthread -g hello.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
cl : Command line error D8004 : '/W' requires an argument
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\BIN\cl.EXE"' : return code '0x2'
Stop.
EDIT
I have also tried the to compile like instructed in the user manual which on windows should translate to "cl hello.c mongoose.c -o hello.exe" but then I get the following error message:
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
cl : Command line warning D9035 : option 'o' has been deprecated and will be removed in a future release
hello.c
mongoose.c
Generating Code...
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:hello.exe
/out:hello.exe
hello.obj
mongoose.obj
hello.obj : error LNK2019: unresolved external symbol _snprintf referenced in function _begin_request_handler
hello.exe : fatal error LNK1120: 1 unresolved externals
Does anyone have suggestions on what steps needs to be taken in order to compile the hello.c example for Mongoose under Windows?

I found out that the issue with my third try above was that "_snprintf" has been depreached and replaced by "_snprintf_s" in the C version used by Visual Studio 10. I thus replaced the one occurence of "_snprintf" with _snprintf_s and it worked.

It looks like you are not specifying the linkage to the library (DLL, whatever it actually is) for mongoose. That's why you have the unresolved externals. It needs to know where to find them so that the executable can locate them at runtime (if it's linked dynamically) or include the code in the .exe (if done statically).

Related

How to write makefile(nmake) for visual studio build tool cl.exe

I want to build a c application with visual-studio build tool cl.exe by invoking nmake similar like make in **NIX systems. I have written a makefile. It cannot find include files and object files for linking. I couldn't find much reference how to link libraries.
CC=cl.exe
INC=-I../include
LIB=-L../lib
socket: socket.c
$(CC) socket.c $(INC) $(LIB) -lssl -lcrypto
output
D:\client>nmake
Microsoft (R) Program Maintenance Utility Version 10.00.30319.01
Copyright (C) Microsoft Corporation. All rights reserved.
cl.exe socket.c -I../include -L../lib -lssl -lcrypto
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
cl : Command line warning D9002 : ignoring unknown option '-L../lib'
cl : Command line warning D9002 : ignoring unknown option '-lssl'
cl : Command line warning D9002 : ignoring unknown option '-lcrypto'
socket.c
Microsoft (R) Incremental Linker Version 10.00.40219.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:socket.exe
socket.obj
LINK : fatal error LNK1104: cannot open file 'Ws2_32.lib'
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\BIN\cl.exe"' : return code '0x2'
Stop.
the cl.exe does not use the gcc syntax for libraries and include files.
Here is a (usable but not perfect) example of how to specify the files:
cl main.c freetype.lib gdi32.lib glew.lib jpeg.lib
you might also want to read how to include in cl.exe

Windows cmake cannot find memset or other standard functions

I am compiling a collection of packages using cmake, often for which I write my own CMakeLists.txt. I have to compile them both using Visual Studio 9 2008 and Visual Studio 14 2015
For VS2015 compilation often completes without problems, but with VS2008 I frequently bump into the problem that cmake doesn't link to or include some standard msvc libraries (or msvc runtime libraries to be specific). As a result, it cannot find memset() if it is called by a module. One of these modules is glew, which has the following output:
Scanning dependencies of target glew
[ 8%] Building C object CMakeFiles/glew.dir/C_/Users/Daniel/OneDrive/src/glew-2.0.0/src/glew.c.obj
glew.c
[ 16%] Building RC object CMakeFiles/glew.dir/C_/Users/Daniel/OneDrive/src/glew-2.0.0/build/glew.rc.res
Microsoft (R) Windows (R) Resource Compiler Version 6.1.7600.16385
Copyright (C) Microsoft Corporation. All rights reserved.
[ 25%] Linking C shared library bin\glew32.dll
Creating library lib\glew32.lib and object lib\glew32.exp
glew.c.obj : error LNK2019: unresolved external symbol memset referenced in function glewContextInit
bin\glew32.dll : fatal error LNK1120: 1 unresolved externals
LINK failed. with 1120
NMAKE : fatal error U1077: 'C:\ProgramData\chocolatey\lib\cmake.portable\tools\cmake-3.6.1-win32-x86\bin\cmake.exe' : return code '0xffffffff'
Stop.
NMAKE : fatal error U1077: '"C:\Users\Daniel\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Users\Daniel\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\nmake.exe"' : return code '0x2'
Stop.
I know I have to include the libcmt.lib or msvcrt.lib (+headers) to the project, but I have no idea how to. I found some leads here and here but none have lead me anywhere so far.
Is there also a way I could do this when calling cmake from the command line (and thus without having to edit or patch the CMakeLists.txt files themselves)?
Thanks for any insights!
Remove the linker -nodefaultlib option; this can be set in the IDE GUI at Linker / Input / Ignore All Default Libraries / No

C - Unable to use MSVC linker to link .obj files from the command line

I have been trying to follow the instructions at http://llvm.org/docs/GettingStartedVS.html#an-example-using-the-llvm-tool-chain, but I run into errors at the linking stage. For some reason, I can compile the "hello world" program all the way to an .exe inside the Visual Studio GUI, but when I run the compiler (either clang or cl) from the command line, I get strange errors. I can create .obj objects just fine, but linking produces errors like the following:
link /DEFAULTLIB:libcmt /VERBOSE hello.obj
Microsoft (R) Incremental Linker Version 12.00.31101.0
Copyright (C) Microsoft Corporation. All rights reserved.
Starting pass 1
Searching libraries
Searching C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\LIB\libcmt.lib:
Finished searching libraries
Finished pass 1
Unused libraries:
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\LIB\libcmt.lib
hello.obj : error LNK2019: unresolved external symbol printf referenced in function main
LINK : error LNK2001: unresolved external symbol mainCRTStartup
hello.exe : fatal error LNK1120: 2 unresolved externals
For some reason, it is ignoring the specified library and is unable to locate the relevant libraries for linking. How can I get the linker to resolve the symbols properly?

C: LINK.EXE fails from Makefile but not from the Command line

When I attempt to link from a makefile I get the following error:
LINK : fatal error LNK1104: cannot open file 'LIBCMT.lib'.
Makefile Execution:
C:\Users\snmcdonald\Desktop\winMake2\winMake2>nmake "_DEBUG=" /f win2.mk build
Microsoft (R) Program Maintenance Utility Version 10.00.30319.01
Copyright (C) Microsoft Corporation. All rights reserved.
cl /c /ZI /Fo"Debug\\" /Fe"Debug\\" main.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
main.c
cl /c /ZI /Fo"Debug\\" /Fe"Debug\\" lib.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
lib.c
lib Debug\lib.obj /out:Debug\lib.lib
Microsoft (R) Library Manager Version 10.00.30319.01
Copyright (C) Microsoft Corporation. All rights reserved.
link Debug\main.obj Debug\lib.lib /out:Debug\main.exe
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation. All rights reserved.
main.obj : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/OPT:ICF' specif
ication
LINK : fatal error LNK1104: cannot open file 'LIBCMT.lib'
NMAKE : fatal error U1077: '"C:\Program Files\Microsoft Visual Studio 10.0\VC\BI
N\link.EXE"' : return code '0x450'
Stop.
However, if I rerun the exact same line that failed and link from the console I get a successful build. I am using the exact same lib and obj that were produced from my make file.
Console Execution:
C:\Users\snmcdonald\Desktop\winMake2\winMake2>link Debug\main.obj Debug\lib.lib /o
ut:Debug\main.exe
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation. All rights reserved.
main.obj : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/OPT:ICF' specif
ication
C:\Users\SHANEM~1\Desktop\winMake2\winMake2>debug\main.exe
print from lib
I have included my makefile for reference.
Makefile
!ifdef _DEBUG
CC = cl
CFLAGS = /c /ZI
FILES = *.c
OUT = /Fo"Debug\\" /Fe"Debug\\"
LINKOUT = /out:Debug
DIR = Debug
!else
CC = cl
CFLAGS = /O2
FILES = *.c
OUT = /Fo"Release\\" /Fe"Release\\"
LINKOUT = /out:Release
DIR = Release
!endif
LIB = lib
LINK = link
RM = del
RMFLAGS = *.ojb *.exe 2>NUL
build: main.exe
clean:
$(RM) $(RMFLAGS)
rebuild: clean build
main.exe: main.obj lib.lib
$(LINK) $(DIR)\main.obj $(DIR)\lib.lib $(LINKOUT)\main.exe
lib.lib: lib.obj
$(LIB) $(DIR)\lib.obj $(LINKOUT)\lib.lib
main.obj:
$(CC) $(CFLAGS) $(OUT) main.c
lib.obj:
$(CC) $(CFLAGS) $(OUT) lib.c
Testing
I have tested this on both Visual C version 9 and version 10. I am confused why it would fail on my makefile but run successfully when manually entered on the command line.
Solution:
nmake /E /f win2.mk build
/E - overrides macro vars with environmental paths.
LIB = lib
That screws up the LIB environment variable. Yes, /E will fix it but your next project that actually needs lib.exe is going to fail. Pick another name, win32.mak uses "implib".
The file should exist in
...\Microsoft Visual Studio 8\VC\lib
It could be difference of environment variables setting. Check what are the enviroment variables setting when you run it manually from the command line.
http://us.generation-nt.com/answer/lnk1104-open-file-libcmt-lib-help-21575202.html
The LIB environment variable should
contain the path to your various lib
directories. You can also run the
VCVARS32.BAT file, which will
automatically set the environment up
for you. If you do a lot of command
line builds, I recommend creating a
shortcut that invokes the above
mentioned VSVARS32.BAT

How to avoid "error LNK2005:" (already defined stdlib functions) when compiling libpng with Microsoft Visual Studio '08?

(Why I am trying to do this: I have had no luck in using the pre-built binaries for libpng on Windows (despite many hours of trial and error), and therefore am now trying to compile it myself. I found this helpful blog post concerning this, complete with a Microsoft Visual Studio 2008 project file, but unfortunately have still not been able to use it.)
It successfully compiles when linking to the zlib binary found here, but when attempting to link it to a test file I receive the following:
C:\Documents and Settings\Administrator\My Documents>cl "C:\Documents and Settin
gs\Administrator\My Documents\test.c" "C:\Documents and Settings\Administrator\M
y Documents\libpng.lib" -I "C:\Documents and Settings\Administrator\My Documents\include"
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
test.c
Microsoft (R) Incremental Linker Version 9.00.30729.01
Copyright (C) Microsoft Corporation. All rights reserved.
/out:test.exe
test.obj
"C:\Documents and Settings\Administrator\My Documents\libpng.lib"
MSVCRTD.lib(MSVCR90D.dll) : error LNK2005: _abort already defined in LIBCMT.lib(
abort.obj)
MSVCRTD.lib(MSVCR90D.dll) : error LNK2005: _fread already defined in LIBCMT.lib(
fread.obj)
MSVCRTD.lib(MSVCR90D.dll) : error LNK2005: _malloc already defined in LIBCMT.lib
(malloc.obj)
MSVCRTD.lib(MSVCR90D.dll) : error LNK2005: _free already defined in LIBCMT.lib(f
ree.obj)
MSVCRTD.lib(MSVCR90D.dll) : error LNK2005: ___iob_func already defined in LIBCMT
.lib(_file.obj)
LINK : warning LNK4098: defaultlib 'MSVCRTD' conflicts with use of other libs; u
se /NODEFAULTLIB:library
test.exe : fatal error LNK1169: one or more multiply defined symbols found
Could anyone care to shed some light as to how to fix these errors?
The problem is that one of the two projects is using Multi-threaded DLL while the other is using Multi-threaded (without DLL in it) in Project Properties -> Configuration Properties -> C/C++ -> Code generation.
Happened to me several times.
Make sure to "synchronize" that setting even for release builds, and for each possible combination (debug, non debug, etc)

Resources