Tag Archives: c++

error C4996: ‘inet_addr‘: Use inet_pton() or InetPton() instead or define _WINSOCK_D

[questions]

Vs2015 appears when writing socket client. (this is the second time I have encountered this problem. Last time vs2017, I must remember it to facilitate myself to check in the future: P)

SOCKADDR_ IN ClientAddr;
ClientAddr.sin_ family = AF_ INET;
ClientAddr.sin_ addr.S_ un.S_ addr = inet_ addr(“127.0.0.1”);
ClientAddr.sin_ Port = htons (port);
error c4996: ‘INET_ addr’: Use inet_ pton() or InetPton() instead or define _ WINSOCK_ DEPRECATED_ NO_ WARNINGS to disable deprecated API warnings

[reason]

In the version after vs2013, INET is added_ New functions such as Pton () and inetpton () are used for IP address conversion between “dotted decimal” and “binary integer”, and can handle IPv4 and IPv6. And INET_ Addr is an old function. Higher version vs uses a new function by default when compiling, so this error will be reported.

[solution 1]

Replace INET with the new function suggested_ Addr function.

[solution 2]

Modify the vs configuration and tell it I want the old function

1. Modification method: Item – & gt; attribute – & gt; C / C + + – & gt; general – & gt; SDL check, change “yes” to “no”.
2. Property page of file_ CRT_ SECURE_ NO_ “Warnings” plus
– – –
copyright notice: This is CSDN blogger Rani_ The original article of “ZZ” follows CC 4.0 by-sa copyright agreement. Please attach the link of original source and this statement.
Link to the original text: https://blog.csdn.net/weixin_ 42731241/article/details/96143934

Solution: about the problem “C + + – unresolved inclusion: < iostream >“

Problem description

Develop C + + program with eclipse for C + + and CDT plug-in and compilation tool MinGW, write in the head of C + + source program

#include <iostream>
#include <stdio>

When you wait for such a standard library, the editor will prompt:

Unresolved inclusion: <iostream>

resolvent

First, select the project name, in the selection menu: Project & gt; properties & gt; C / C + + general & gt; preprocessor includes ≫ providers
and check “CDT GCC build in compiler settings”, then click the Apply button.

I’ve tried other solutions on the Internet, but they don’t work.

VC + + compiler can not find the header file and rebuild failure

1. The header file cannot be found

If the associated path of the header file is not set properly, you need to add both the root directory and the subdirectory to the additional header file directory. For example, if there are two header files in the root directory and the subdirectories sub1 and sub2 that need to be referenced elsewhere, you should add the header file path to the additional package directory in this way:

$(SolutionDir)root

$(SolutionDir)root\sub1

$(SolutionDir)root\sub2

2. It is successful to compile a library alone, but rebuild prompts that the related projects fail to compile

In this case, the lazy relationship between project references is not set well. You should right-click on the solution, – gt; common properties – & gt; project dependencies, and set the lazy relationship between project references without omission. In this way, the rebuild will succeed on the top-level project. At the same time, the compilation of dynamic library and other similar problems can also be solved.

 

 

 

The use of C + + template function and lambda expression

#include <iostream>

using namespace std;

class Tmp1 {
public:
    int foo() { 
        cout << "Tmp1.foo" << endl;
        return ret;
    }

    string walk() {
        cout << "walk" << endl;
        return "cout walk";
    }

    int ret = 1;
};

class Tmp2 {
public:
    int foo() {
        cout << "Tmp2.foo" << endl;
        return ret;
    }

    string run() {
        cout << "run" << endl;
        return "cout run";
    }

    int ret = 2;
};

struct OutPara {
    int ret = 0;
    string desc;
};

template<typename T, typename F>
void print(T t, const F &f) {
    cout << "test" << endl;
    f(t);
}

int main()
{
    Tmp1 t1;
    OutPara out;
    print(t1, [&](Tmp1 &t)->void{
        out.desc = t.walk();
        out.ret = t.foo();
    });
    cout << out.ret << "\t" << out.desc << endl;
    Tmp2 t2;
    print(t2, [&](Tmp2 &t)->void{
        out.desc = t.run();
        out.ret = t.foo();
    });
    cout << out.ret << "\t" << out.desc << endl;
    return 0;
}

The output

test
walk
Tmp1.foo
1       cout walk
test
run
Tmp2.foo
2       cout run

Error LNK2019: unresolved external symbol_ Main the symbol is in the function___ Tmaincrtstartup

Error:
Error LNK2019: Unable to resolve external symbol _main, which is referenced in the function ___TMainCrtStartup
Cause analysis,
I use VS2013 debugging an MFC program when the problem occurred.
1. You built a console program with VC. Its entry function should be main, and you used WinMain.

    > You open a.c/.cpp file with vc and compile it directly, using Winmian instead of main as the entry function. VC’s default Settings at this point are for console applications.

Solutions:
1. Enter the project – & gt; setting-> C/C ++, select Preprocessor in Category, remove _CONSOLE in Processor Definitions, and add _WINDOWS
2. Enter the project – & gt; setting-> Link changes /subsystem:console to /subsystem: Windows in Projections.
3. Save Settings, Rebuild All.

Clion automatically adds add_ executable

CLion automatically adds add_executable
Install the C/C++ Single File Execution plugin, search for the C/C++ Single File Execution, find file-settings-plugins, and apply.
The preparations are complete. Next you can create a new C/ CPP file, enter the code, press Ctrl + Alt + Shift +E shortcut or right click in the code area and find Add Executable for Simple C/C++ File, then right click on the item area on the left and select Reload Cmake Project, select Auto-Reload so that it will automatically add to the executable after each right click on Add.

There is a problem of DLL when executing G + + compile command in sublime text editor

Sublime Text sets GCC and g++ (g++ is a superset of GCC, so just g++), most commonly the Mingw folder in dev-cpp.
The problem is as follows:
contains the source file for the input function, which can be compiled in Devc, and a popup window in the sublime text environment shows that the file entry cannot be found and the program input point __gxx_personality_v0 cannot be located in the dynamic link library.

Dynamic Link Library problem caused by DLL files.
Return to the Mingw file where the environment variable was imported and copy the libstdc++6.dll from the lib file.

br>

(Suggestions)
② Paste into the directory where the source file is located.
If there are mistakes, please correct them.

The C compiler identification is unknown

The environment
CLion+Cmake+Mingw
An error log

-- The C compiler identification is unknown
-- Check for working C compiler: D:/MinGW/bin/gcc.exe
-- Check for working C compiler: D:/MinGW/bin/gcc.exe -- broken
CMake Error at D:/Program Files/JetBrains/CLion 2019.2.3/bin/cmake/win/share/cmake-3.15/Modules/CMakeTestCCompiler.cmake:60 (message):
  The C compiler

    "D:/MinGW/bin/gcc.exe"

  is not able to compile a simple test program.

If it is a CPP program, it may be reported again

-- The CXX compiler identification is unknown

why
CMAKE could not check the C Compiler ID for unknown reasons, causing compilation to fail
The solution
Add the following configuration to the cMakelists.txt file to force the Compiler ID to be specified

INCLUDE(CMakeForceCompiler)
CMAKE_FORCE_C_COMPILER(gcc GNU)
CMAKE_FORCE_CXX_COMPILER(g++ GNU)

Rerun the cmake directive after adding it

Using Visual Studio 2015 to create. DLL and use. DLL

Use Visual Studio 2015 to create.dll and use.dll
In this paper, the implementation of an addition and meet function is taken as an example:
Create a DLL project:
1, File — New — Project — Win32 console application. Give it a name, like myadd. Click OK. Next, select DLL(D) for the application type and click Finish.
>
>
>
>
>
>
>
>
> You can delete it. Create a new one. CPP, name “mydll. CPP”, create a new “mydll.h”
3, write an addition and subtraction function for example; In mydll. CPP, write the following:

#include"stdafx.h"
#include"mydll.h"
int add(int a,int b)
{
    return a+b;
}

int sub(int c,int d)
{
    return c-d;
}

4. Write the following in mydll.h:

#pragma once
_declspec(dllexport)int add(int a, int b);
_declspec(dllexport)int sub(int a, int b);

5. Build — Build the solution. (select Debug X86 mode) and it will be in the project root directory (with.sln directory), under the Debug folder. Generate.dll and.lib files.

> File — New — Project — Win32 Console Application Give it a name like MyDllTest, click OK, Next, Console Application (W), Empty Project (E), Finish;
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> Right click, add an existing item, and add mydll.h to the header file.

#include<iostream>
#include "mydll.h"
using namespace std;

int main()
{

    int ad, su;
    ad = add(100, 200);
    su = sub(10, 20);
    cout << "ad: " << ad << endl;
    cout << "su: " << su << endl;
    getchar();
    return 0;
}

5, in the project “resource file” right click, add – existing item; Select the.lib file and click OK.
6, Build, Solution, Run That’s it.

Collect2: error: LD returned 1 exit status

After verification, one of the reasons caused the factors is:
that you declare a function name, and in. H file defines some methods, but not in. CPP implementation inside, this will appear such mistakes.
and the solution can refer to the following:

the above for the nsight eclipse compiler error, as is shown in the display under the compilation instructions. O file is false, do not implement the method can search and in the documents. H file compared to completion.