c++ - 当包含 SDL 时,简单的 tcp echo 程序不工作?

标签 c++ c runtime sdl winsock

我有一个奇怪的问题,每当我 #include "SDL/SDL.h" 时,我的 windows 套接字程序就不会执行。它编译但在运行时不执行任何操作。当我删除 #include "SDL/SDL.h" header 、编译并运行时,它又开始工作了??。

我正在努力使 SDL 和我原来的套接字程序都能正常工作,但我不明白这是怎么回事。

//#include "SDL/SDL.h"
#define _WIN32_WINNT 0x501
#include <iostream>
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <string.h>
#include <ws2tcpip.h>
#define MAXLEN 80
using namespace std;

const int winsockVersion = 2;

int main( int argc, char* args[] ) { 

    WSADATA wsadata;
    if ( (WSAStartup(MAKEWORD(2,0),&wsadata)) == 0){
        cout<<"-[ WSAStartup Initialized. ]" << endl;

        char PORT[MAXLEN];
        char SERVER[MAXLEN];
        cout <<"Server: ";
        cin>>SERVER;
        cout <<"Port: ";
        cin>>PORT;

        struct addrinfo hints, *res;
        int sockfd;

        memset(&hints,0,sizeof hints);
        hints.ai_family = AF_UNSPEC;
        hints.ai_socktype = SOCK_STREAM;

        if (getaddrinfo(SERVER,PORT,&hints,&res) != 0){
            cout<<"-Getaddrinfo unsuccessful." << endl;
        }

        if ( (sockfd = socket(res->ai_family,res->ai_socktype,res->ai_protocol)) == -1 ){
            cout<<"-Unable to create socket." << endl;
        }

        if ( (connect(sockfd,res->ai_addr,res->ai_addrlen)) != -1 ){
            cout<<"-[ Connection Established. ]" << endl;
        }

        cout<<"-[ Client connecting to: ]" << res->ai_addr << endl;

        while(true){
            string text_buff;
            cout<<"Send: ";
            getline(cin,text_buff);
            if( (send(sockfd,text_buff.c_str(),text_buff.length()+1,0)) != -1 ){
                cout<<"-----------------------------------> Data sent!." << endl;
            }

            if ( text_buff == "quit" ){
                break;
            }

        }

    }else{
        cout<<"-WSAStartup Initialization failed." << endl;
        if(WSACleanup()!=0){
            cout<<"-WSACleanup Successful." << endl;
        }else{
            cout<<"-WSACleanup Failed." << endl;
        }
    }

    return 0;
}

编译

g++ -o draft.exe draft.cpp -I"C:\compilers and libs\Libs\SDL\SDL devep\SDL-1.2.15\include" -L"C:\compilers and libs\Libs\SDL\SDL devep\SDL-1.2.15\lib" -lmingw32 -lSDLmain -lSDL -lws2_32

最佳答案

据我所知,SDL 用一些愚蠢的#define main 技巧重写了 main 函数,它像那样拦截程序 main。

因此,可能根本不会调用 main。

如果我没记错的话,你的 main 函数(参数)应该完全 int main(int argc, char *argv[])

关于c++ - 当包含 SDL 时,简单的 tcp echo 程序不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10877885/

相关文章:

c++ - 检查失败后,Boost Unit Test将所有控制台文本永久变为红色

c++ - 通过变量名遍历结构

c++ - 如果前一个线程返回不同的类型,如何将数据发送到下一个线程?

c - 如何在 C 中将矩阵中的形状旋转 90 度?

c - C 运行时的功能和包含的内容

C++ 文件作用域静态函数

c - OpenCV 较低的颜色值

c - qsort-C标准库-如何发送带有非const参数的比较函数?

c++ - 运行时分析单个 for 循环,其中 i 不递增 1

ubuntu - 基于 Alpine 的 Docker 镜像在 Ubuntu 主机上完全兼容?