[Compile] 2, MinGW gcc in Windows build Win32 program Environment

Source: Internet
Author: User
Tags readfile


1, ordinary download a MinGW program, after installation can directly copy the MinGW directory to the main project tool inside:

 demo_mesh_common tree -L 2.├── app├── bin├── build├── doc├── sdk│?? ├── alg│?? ├── bsp│?? ├── driver│?? └── phy└── tool    └── MinGW


2, the reference learning in DOS using GCC to compile, found that the step-up compilation will report _alloca undefined error:

a.o:a.c:(.text+0x3f8): undefined reference to `_alloca'

The guess is that some libraries do not have links in, so search for libraries under mingw/lib/:

F:\demo_mesh_common\tool\MinGW\lib>grep -rn "_alloc"Binary file libmingw32.a matchesBinary file libmingwex.a matchesBinary file libwldap32.a matchesBinary file libdxerr8.a matchesBinary file libdxerr9.a matchesBinary file gcc/mingw32/3.4.5/libgcc.a matchesBinary file librpcdce4.a matchesBinary file libbfd.a matchesBinary file libiberty.a matches

With an Add-in compilation option, the final discovery is: You need to add GCC/MINGW32/3.4.5/LIBGCC.A:

./MinGW/bin/ld -L"./MinGW/lib" -o a.exe a.o  "./MinGW/lib/crt2.o" -lmingw32 -lkernel32 -lmsvcrt -luser32 -lwow32 -lwldap32 -lwin32k -lvfw32 -lmingw32 -lmingwex -lwldap32 -ldxerr8 -ldxerr9 -lrpcdce4 -lbfd -liberty -L"./MinGW/lib/gcc/mingw32/3.4.5" -lgcc


3, I test the Win32 serial port program, in the Git Bush (Linux properties) window can be executed, but no data, must be executed in the win window.


4. Appendix

Makefile

a.o:a.c    ./MinGW/bin/gcc -c a.cB:a.o    ./MinGW/bin/ld -L"./MinGW/lib" -o a.exe a.o  "./MinGW/lib/crt2.o" -lmingw32 -lkernel32 -lmsvcrt -luser32 -lwow32 -lwldap32 -lwin32k -lvfw32 -lmingw32 -lmingwex -lwldap32 -ldxerr8 -ldxerr9 -lrpcdce4 -lbfd -liberty -L"./MinGW/lib/gcc/mingw32/3.4.5" -lgccA:    ./MinGW/bin/gcc -s -O2 a.c -mconsole

A.C:

#include <stdio.h> #include <stdlib.h> #include <windows.h> #include <string.h> #include < Malloc.h>handle hcomm;overlapped M_ov; Comstat Comstat;dword m_dwcommevents;//If the FILE_FLAG_OVERLAPPED flag is defined when calling CreateFile to create a handle Then calls to ReadFile and WriteFile to the handle in the//row should be overlapping, if the//overlap flag is not specified, then the read and write operation should be synchronous//at synchronous execution, the function will not return until the operation is completed. This means that the thread is blocked while the synchronization is executing, resulting in a decrease in efficiency from//. When overlapping execution, even if the operation is not completed, the two functions will return immediately, the fee//when the I/O operation in the background bool Openport (char *portname)//Open a serial port {Hcomm = CreateFile (PortName, Gener Ic_read |    Generic_write, 0, 0, open_existing, file_flag_overlapped, 0);    if (Hcomm = = INVALID_HANDLE_VALUE) return FALSE; else return TRUE;}    BOOL SETUPDCB (int rate_arg) {DCB DCB;    int rate= Rate_arg; memset (&dcb,0,sizeof (DCB)); Populating a given value in a block of memory is the quickest way to clear 0 operations on a larger structure//body or array if (!    Getcommstate (HCOMM,&AMP;DCB))//Get current DCB configuration {return FALSE;     }/*--------------------------------------------------------------------*//Set DCB to configure the serial port Dcb. DCBlength = sizeof (DCB); /*----------Serial Port Config-------*/DCB.    BaudRate = rate; Dcb.    Parity = noparity;    dcb.fparity = 0; Dcb.    StopBits = Onestopbit; Dcb.    ByteSize = 8;    Dcb.foutxctsflow = 0;    Dcb.foutxdsrflow = 0;    Dcb.fdtrcontrol = dtr_control_disable;    dcb.fdsrsensitivity = 0;    Dcb.frtscontrol = rts_control_disable;    DCB.FOUTX = 0;    Dcb.finx = 0;    /*-----------------Misc Parameters-----*/Dcb.ferrorchar = 0;    Dcb.fbinary = 1;    Dcb.fnull = 0;    Dcb.fabortonerror = 0;    dcb.wreserved = 0; Dcb.    Xonlim = 2; Dcb.    Xofflim = 4; Dcb.    XonChar = 0x13; Dcb.    XoffChar = 0x19; Dcb.    Evtchar = 0; /*--------------------------------------------------------------------*//Set DCB if (!    SetCommState (HCOMM,&AMP;DCB)) {return FALSE; } else return TRUE;} Read and write serial ports with ReadFile and WriteFile, you need to consider the time-out problem, read and write serial port timeout has two//species: Interval timeout and total timeout, write operation only supports the total timeout, and read operation two timeouts are supported, if all//write timeout parameters are 0, then the write timeout is not used. BOOL setuptimeout (DWORD readinterval,dword readtotalmultiplier,dword Readtotalconstant,dword WriteTotalMultiplier,    DWORD writetotalconstant) {commtimeouts timeouts; Timeouts. Readintervaltimeout=readinterval; Read interval timeout timeouts. Readtotaltimeoutconstant=readtotalconstant; Read time factor timeouts. Readtotaltimeoutmultiplier=readtotalmultiplier; Read time constant timeouts. Writetotaltimeoutconstant=writetotalconstant; Write time factor timeouts. Writetotaltimeoutmultiplier=writetotalmultiplier; Write Time constant//volume, the total timeout is calculated as: Total Timeout = time factor x number of characters required to read/write + TIME constant if (!    SetCommTimeouts (Hcomm, &timeouts)) {return FALSE; } else return TRUE;}    void Receivechar () {BOOL bRead = TRUE;    BOOL bresult = TRUE;    DWORD dwerror = 0;    DWORD bytesread = 0;    Char Rxbuff;    while (TRUE) {bresult = Clearcommerror (Hcomm, &dwerror, &comstat); You should use the Clearcommerror function to clear the error if (COMSTAT.CB) before using the ReadFile function for read operations.inque==0)//COMSTAT structure returns the serial port status information//This article only uses the Cbinque member variable, the value of this member variable represents the number of bytes in the input buffer continue; if (bRead) {bresult = ReadFile (Hcomm,//Handle to COMM port serial port handle &rxbuff,//RX Buffer pointer//read-in number According to the stored address, the read-in data is stored/stored in the memory area of the first address with the value of the pointer 1,//the number of bytes of data to read in read one byte, &bytesread,//Stores numbers O           F bytes read, which points to a dword//value that returns the number of bytes actually read in the read Operation &m_ov);        When the pointer to the M_ov structure//overlap operation, this parameter points to a overlapped structure, which is null when synchronizing the operation ("%02x", Rxbuff); if (!bresult) {///when ReadFile and WriteFile return false, it is not necessarily an operation loss//defeat, the thread should call the GetLastError function to parse the returned result switch (dwerror = Getlaste                    Rror ()) {case error_io_pending:bread = FALSE;            Break            Default:break;        }}else{bRead = TRUE;        }}//Close if (bRead) if (!bread) {bRead = TRUE;   Bresult = GetOverlappedResult (Hcomm,//Handle to COMM Port &m_ov, Overlapped structure &bytesread,//Stores number of bytes read TRUE);    Wait flag}}}bool Writechar (byte* m_szwritebuffer,dword m_ntosend) {BOOL bwrite = TRUE;    BOOL bresult = TRUE;    DWORD bytessent = 0;    HANDLE m_hwriteevent;    ResetEvent (m_hwriteevent); if (bwrite) {M_ov.    Offset = 0; M_ov.    Offsethigh = 0; Clear Buffer bresult = WriteFile (Hcomm,//Handle to COMM Port, handle of serial port M_szwritebuffer,//Pointer T                                o message buffer in calling Finction//That is, the value of the pointer to the first address Nnumberofbytestowrite bytes of data will be written to the serial port of the Send data buffer m_ntosend,//Length of message to send, the number of bytes to write data &am P BytesSent,//Where to store the number of bytes sent//point to a DWORD value that returns                The number of bytes actually written &m_ov); Overlapped structure//overlap operation, this parameter points to a Overlapped structure,//synchronous operation, this parameter is null if (!bresult) {///when ReadFile and WriteFile return False,        The thread should call the GetLastError function to parse the returned results DWORD dwerror = GetLastError (); Switch (dwerror) {case error_io_pending://getlasterror function returns//error_io_pending.                This indicates that the overlap operation has not yet completed//continue to getoverlappedresults () bytessent = 0;                Bwrite = FALSE;        Break        Default:break;    }}}//End if (bwrite) if (!bwrite) {bwrite = TRUE; Bresult = GetOverlappedResult (Hcomm,//Handle to COMM Port &m_ov,//Overlapped structure &am P             BytesSent,//Stores number of bytes sent TRUE);    Wait Flag//deal with the error code if (!bresult) {printf ("getoverlappedresults () in WriteFile ()"); }}//End if (!bwrite)//Verify that the data size of send equals what we tried to send if (bytessent! = m_  Ntosend) {  printf ("Warning:writefile () Error: Bytes Sent:%d;    Message Length:%d\n ", BytesSent, strlen ((char*) m_szwritebuffer)); } return TRUE;    int main (void) {printf ("Open comport successsdsfds\n");    if (Openport ("COM3")) printf ("Open comport success\n");    if (SETUPDCB (9600)) printf ("Setupdcb success\n");    if (Setuptimeout (0,0,0,0,0))///If all write timeout parameters are 0, then the write timeout of printf ("Setuptimeout success\n") is not used; PurgeComm (Hcomm, Purge_rxclear | Purge_txclear | Purge_rxabort | Purge_txabort);    Before reading and writing the serial port, also use the PurgeComm () function to empty the buffer//purge_txabort interrupt all writes and return immediately, even if the write operation is not completed.    Purge_rxabort interrupts all read operations and returns immediately, even if the read operation has not been completed.    Purge_txclear clears the output buffer//purge_rxclear clears the input buffer//writechar ("Please send the data now", 20);    printf ("Received data:\n");    Receivechar ();    System ("pause"); return 0;}

Link
    • Learn to use GCC to compile under DOS: https://www.cnblogs.com/personnel/p/4584819.html

@beautifulzzzz智能硬件、物联网,热爱技术,关注产品博客:http://blog.beautifulzzzz.com园友交流群:414948975

[Compile] 2, MinGW gcc in Windows build Win32 program Environment

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.