Extract the GCC-like compiler from the VS file and compile the C program as the dll dynamic link library.

Source: Internet
Author: User
Tags microsoft c
I'm used to the GCC compilation tool under linux, and I prefer to compile and link C programs from the command line. I actually found that the VS tool I installed did not see a tool that directly compiles C programs (I'm not familiar with this). VS's compile file command is cl and the link command is link. With reference to online materials, a tool that compiles programs from the command line is formed by extracting VS compiler files. Here are the steps:
1. Create a folder in your own directory, such as: D: / vctool;
2. Test the bin, lib, include directories in the VC directory under the VS installation directory to the vctool directory;
3. Copy the Lib directory under c: / Program Files / Microsoft SDKs / Windows / V6.0A (or similar directory, also the installation file of VS) to the vctool directory, and remember not to overwrite but rename it to sdklib;
4, now the required command files, class libraries, include files are copied over, add the bin directory to the environment variable path;
5. Next, you can write the C language file Test.c:
#include "stdio.h"
__declspec (dllexport) int sum (int a, int b)
{
        return a + b;
}

5. Write the compilation file: build.bat:
set PATH =. /; D: / vctool / bin;% path%
set INCLUDE = D: / vctool / include
set LIB = D: / vctool / lib; D: / vctool / sdklib

cl / c Test.c
link / dll Test.obj
pause

6, double-click to run build.bat, found compilation errors, can not find mspdb80.dll file:


Then search in the VS installation directory and copy the file to the vstool / bin directory; then double-click build.bat to compile.
Test.dll Test.lib Test.obj

If LINK: fatal error LNK1104: Unable to open the file "kernel32.lib" occurs during linking, it is because sdklib is not copied or set to the environment variable LIB (build.bat)

The following is the relevant information found online:
2. The cl command is described in detail below:
        cl.exe is a 32-bit tool that controls the Microsoft C and C ++ compilers and linkers. Compiler produces Common Object File Format (COFF)
Object (.obj) files. The linker produces an executable file (.exe) or a dynamic link library file (DLL).
        Note that all compiler options are case sensitive.
        The usage is as follows:
        CL [option ...] file ... [option | file] ... [lib ...] [@ command-file] [/ link link-opt ...]
        option: One or more CL options. Note that all options apply to all specified source files. Options are specified by a forward slash (/) or a dash (-). If an option has parameters, the option's description specifies whether spaces are allowed between the option and the parameter. Option names (except the / HELP option) are case sensitive. CL:
        / c: compile but not link
        / Zs: check syntax
        /?: List compiler options
        / arch: Generate code using SSE or SSE2 instructions
        / arch: SSE2
        / clr: Enables a managed extension for C ++ and produces an output file that runs on the common language runtime
        / F: Set stack size | This option sets the program stack size in bytes. If this option is not used, the default stack size is 1 MB, for example: / F 100
        / Fe: Rename the executable
        /Feexe.exe
        / G7: Optimize code for Pentium 4 or Athlon.
        / GA: Optimize code for Windows applications
        / GL: enable full program optimization
        / Gm: enable minimal regeneration
        / HELP: list compiler options
        / I: Search for include files in the directory | Specify source file directory include, for example: / I "e: \ VC \ include"
        / link: pass the specified options to LINK
        / MD: Create a multithreaded DLL using MSVCRT.lib
        / ML: Create a single-threaded executable using LIBC.lib
        / MT: Create a multi-threaded executable using LIBCMT.lib
        / O1: create small code
        / O2: Create quick code
        / Os: code size first
        / W: set warning level
        / W3
        / Ot: code speed priority
        / LD: create dynamic link library
        / Ox: use maximum optimization
        / TC: specify C source file
        / TP: specify C ++ source files
        -------------------------------------------------- ---------------------------------------
        file: The name of one or more source files, .obj files, or libraries. CL compiles the source files and passes the names of the .obj files and libraries to the linker. For more information, see CL File Name Syntax.
        lib: one or more library names. CL passes these names to the linker.
        command-file: A file containing multiple options and file names. For more information, see the CL command file.
        link-opt: one or more linker options. CL passes these options to the linker
        -------------------------------------------------- -------------------------------------------
        Classic usage:
        cl * .cpp / G7 / MD / Ox / Ot / W3 / c / EHsc / I "G: \ Visual C ++ \ VC98 \ PlatformSDK \ Include"

3. Speaking of cl, I have to say another tool link
        link is a 32-bit tool that links Common Object File Format (COFF) object files and libraries to create 32-bit executable (.exe) files or dynamic link libraries (DLLs).
        The link usage is as follows:
        / ALIGN: specify the alignment of each section
        / ALIGN: The option specifies the alignment of each section in the linear address space of the program. The number parameter is in bytes and must be a power of two. The default is 4K (4096). The linker issues a warning if the alignment produces an invalid image. Unless you are writing an application such as a device driver, you should not need to modify the alignment.
        / DLL: Generate DLL
        / DRIVER: create a Windows NT kernel-mode driver
        / EXETYPE: Generate virtual device driver
        / EXETYPE: DYNAMIC creates a dynamically loaded virtual device driver.
        / EXETYPE: DEV386 creates a statically loaded virtual device driver. This is the default for / EXETYPE
        / INCREMENTAL: control incremental linking
        / LARGEADDRESSAWARE: Tell the compiler that the application supports addresses larger than 2 GB
        / LIBPATH: allow users to rewrite the environment library path
        / LIBPATH: "e: \ VC \ LIB" / LIBPATH: "e: \ DX \ LIB"
        / MACHINE: Specify target platform
        / MACHINE: {AM33 | ARM | EBC | IA64 | M32R | MIPS | MIPS16 | MIPSFPU | MIPSFPU16 | MIPSR41XX | SH3 | SH3DSP | SH4 | SH5 | THUMB | X86 | X64 / * 8.0version * /}
        / MACHINE: X86
        / NOENTRY: Create a resource-only DLL. The / NOENTRY option is required when creating a resource-only DLL.
        / OPT: Controls LINK optimization. If building components that run only on Windows NT or Windows 2000, / OPT: NOWIN98 should be used.
        / SUBSYSTEM: specify subsystem {CONSOLE | EFI_APPLICATION | EFI_BOOT_SERVICE_DRIVER |
EFI_ROM | EFI_RUNTIME_DRIVER | NATIVE | POSIX | WINDOWS | WINDOWSCE} [, # [. ##]]
        / OUT: Specify the output file name, for example: /OUT:Hello.exe
        -------------------------------------------------- -------------------------------------------
        Classic usage:
        link * .obj rc.res / LIBPATH: "G: \ Visual C ++ \ lib" / SUBSYSTEM: WINDOWS / MACHINE: X86 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32. lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib OpenGL32.Lib


4. In addition: rc is resource compilation
        / l Lists cultures used by resources
        0x804 China
        0x409 United States
        Usage: rc / l 0x804 file.rc
        -------------------------------------------------- -------------------------------------------
        MFC development is as follows:
        Compile the source code:
        cl * .cpp / Ox / Ot / G7 / D "WIN32" / D "_WINDOWS" / D "_AFXDLL" / EHsc / MD / arch: SSE2 / W3 / c
        Resource compilation:
        rc / l 0x804 winmfc.rc
        Link:
        link * .obj WinMFC.res /out:WinMFC.exe / OPT: NOWIN98 / SUBSYSTEM: WINDOWS / MACHINE: X86
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.