Tag Archives: The c language

[Warning] incompatible implicit declaration of built-in function ‘strcat’

After watching the machine learning course today, I changed to Learn C language for a while. It was also the first time I encountered the strcat function, the string concatenation function. Its general form is strcat (character array 1, character array 2). The function concatenates the strings of two character arrays, attaches string 2 to the end of string 1, and places the result in character array 1. The function call returns a function value — the address of character array 1. Such as:

#include<stdio.h>
int main()
{
	char str1[30] = {"People's Republic of "};
	char str2[] = {"China"};
	printf("%s",strcat(str1,str2));
	
	return 0;
}

Output code error:

In function 'main':
[Warning] incompatible implicit declaration of built-in function 'strcat'

Baidu once reason, little added header file

#include<stdlib.h>

#include<string.h>

After adding, the program output is normal:

C language error: stray ‘\ 240’ in program|

Error source: The code has illegal Ascll code character, illegal space, generally copy the code on the web page to the editor will appear this error.
Solution: According to the error prompt line to find the illegal character, delete it. If the character in the Chinese state below the red wave, directly modify, if the space error, the first block of code to see what part is redundant, delete it. Really can not find the error character, code short can be handwritten.

Linux QT download / usr / include / C + + / 7/ cstdlib:75 : 15: fatal error: stdlib. H: no such file or directory error

The phenomenon of
I recently wrote a small program under Ubuntu that always reports the following errors when running:

/usr/include/c++/7/cstdlib:75:15: fatal error: stdlib.h: No such file or directory #include_next <stdlib.h>


The reason:
I found out that:

This is because GCC7 has included libstdlib.h for better optimization, and the C Library header stdlib.h USES Include_next, which is sensitive to the GCC system header path.

Solutions:
Method 1: remove the following line from the.pro file in Qt:

INCLUDEPATH +=/usr/include

Method 2:
add the following line to the Qt. Pro file:

QMAKE_CFLAGS_ISYSTEM = -I

Reference: https://stackoverflow.com/questions/52532936/usr-include-c-7-cstdlib7515-fatal-error-stdlib-h-no-such-file-or-directo

What should be paid attention to in socket programming — bind socket error: address already in use

When programming Linux networks, you often encounter the following usage errors every time you modify the source code and compile it again:

Bind error: Address already in use

Although the process was forced to end with Ctrl+C, the error still exists. We can still see the process “forced to end” with Ctrl+C with netstat -an |grep 5120 and ps aux |grep 5120. The port is still in use, so we have to kill the process with kill every time, which is very troublesome. Last night, I happened to browse an article titled “5 Hidden Dangers in Linux Socket Programming” on THE IBM website. I suddenly realized that I had a try today and solved the problem as expected. I hereby express my thanks and hope that more coder can read this article and avoid making mistakes.

The main codes are:

int on;

on = 1;
ret = setsockopt( sock, SOL_SOCKET, SO_REUSEADDR, & on, sizeof(on) );
now every time I force the process to end with Ctrl+C, I can still see the port in use with netstat and ps, but the running program will not have the error of “Address already in use”, so the port is reused.

 

Here is the third pitfall in this article — incorrect address usage

Address usage error (EADDRINUSE)

You can use the Bind API function to bind an address (an interface and a port) to a socket endpoint. You can use this function in your server Settings to limit the interfaces that may have a connection coming. You can also use this function in client Settings to limit the interfaces that should be used for outgoing connections. The most common use of BIND is to associate a port number with a server and use a wildcard address (INADDR_ANY), which allows any interface to be used for incoming connections.

A common problem with BIND is trying to bind to a port that is already in use. The trap is that no active socket may exist, but the binding port is still disabled (bind returns EADDRINUSE), which is caused by the TCP socket state TIME_WAIT. This state remains for about 2 to 4 minutes after the socket is closed. After the TIME_WAIT state exits, the socket is deleted and the address can be rebound without problems.

Waiting for TIME_WAIT to end can be annoying, especially if you’re developing a socket server and need to stop the server to make some changes, and then restart. Fortunately, there is a way to get around the TIME_WAIT state. You can apply the SO_REUSEADDR socket option to the socket so that the port can be reused immediately.

Consider the example in Listing 3. Before binding the address, I call setsockopt with the SO_REUSEADDR option. To allow address reuse, I set the integer parameter (on) to 1 (otherwise, I can set it to 0 to prohibit address reuse).

use the SO_REUSEADDR socket option to avoid address usage errors

 

int sock, ret, on;
struct sockaddr_in servaddr;
/* Create a new stream (TCP) socket */
sock = socket( AF_INET, SOCK_STREAM, 0 ):
/* Enable address reuse */
on = 1;
ret = setsockopt( sock, SOL_SOCKET, SO_REUSEADDR, & on, sizeof(on) );
/* Allow connections to port 8080 from any available interface */
memset( & servaddr, 0, sizeof(servaddr) );
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl( INADDR_ANY );
servaddr.sin_port = htons( 45000 );
/* Bind to the address (interface/port) */
ret = bind( sock, (struct sockaddr *)& servaddr, sizeof(servaddr) );
after the SO_REUSEADDR option is applied, the bind API function allows immediate reuse of the address.

 

Five pitfalls in Linux socket programming
http://www.ibm.com/developerworks/cn/linux/l-sockpit/

Linux getsockopt SO_ERROR values (errno.h)

The C function getsockopt lets you get the error codes with the option SO_ERROR.
The possible error numbers are defined in the global errno.h. The relevant values are:

#define ETIMEDOUT   110 /* Connection timed out */
#define ECONNREFUSED    111 /* Connection refused */
#define EHOSTDOWN   112 /* Host is down */
#define EHOSTUNREACH    113 /* No route to host */
#define EALREADY    114 /* Operation already in progress */
#define EINPROGRESS 115 /* Operation now in progress */



//But here's the whole list on my Linux system (/usr/include/asm-generic/errno.h):

#ifndef _ASM_GENERIC_ERRNO_H
#define _ASM_GENERIC_ERRNO_H

#include <asm-generic/errno-base.h>

#define EDEADLK     35  /* Resource deadlock would occur */
#define ENAMETOOLONG    36  /* File name too long */
#define ENOLCK      37  /* No record locks available */
#define ENOSYS      38  /* Function not implemented */
#define ENOTEMPTY   39  /* Directory not empty */
#define ELOOP       40  /* Too many symbolic links encountered */
#define EWOULDBLOCK EAGAIN  /* Operation would block */
#define ENOMSG      42  /* No message of desired type */
#define EIDRM       43  /* Identifier removed */
#define ECHRNG      44  /* Channel number out of range */
#define EL2NSYNC    45  /* Level 2 not synchronized */
#define EL3HLT      46  /* Level 3 halted */
#define EL3RST      47  /* Level 3 reset */
#define ELNRNG      48  /* Link number out of range */
#define EUNATCH     49  /* Protocol driver not attached */
#define ENOCSI      50  /* No CSI structure available */
#define EL2HLT      51  /* Level 2 halted */
#define EBADE       52  /* Invalid exchange */
#define EBADR       53  /* Invalid request descriptor */
#define EXFULL      54  /* Exchange full */
#define ENOANO      55  /* No anode */
#define EBADRQC     56  /* Invalid request code */
#define EBADSLT     57  /* Invalid slot */

#define EDEADLOCK   EDEADLK

#define EBFONT      59  /* Bad font file format */
#define ENOSTR      60  /* Device not a stream */
#define ENODATA     61  /* No data available */
#define ETIME       62  /* Timer expired */
#define ENOSR       63  /* Out of streams resources */
#define ENONET      64  /* Machine is not on the network */
#define ENOPKG      65  /* Package not installed */
#define EREMOTE     66  /* Object is remote */
#define ENOLINK     67  /* Link has been severed */
#define EADV        68  /* Advertise error */
#define ESRMNT      69  /* Srmount error */
#define ECOMM       70  /* Communication error on send */
#define EPROTO      71  /* Protocol error */
#define EMULTIHOP   72  /* Multihop attempted */
#define EDOTDOT     73  /* RFS specific error */
#define EBADMSG     74  /* Not a data message */
#define EOVERFLOW   75  /* Value too large for defined data type */
#define ENOTUNIQ    76  /* Name not unique on network */
#define EBADFD      77  /* File descriptor in bad state */
#define EREMCHG     78  /* Remote address changed */
#define ELIBACC     79  /* Can not access a needed shared library */
#define ELIBBAD     80  /* Accessing a corrupted shared library */
#define ELIBSCN     81  /* .lib section in a.out corrupted */
#define ELIBMAX     82  /* Attempting to link in too many shared libraries */
#define ELIBEXEC    83  /* Cannot exec a shared library directly */
#define EILSEQ      84  /* Illegal byte sequence */
#define ERESTART    85  /* Interrupted system call should be restarted */
#define ESTRPIPE    86  /* Streams pipe error */
#define EUSERS      87  /* Too many users */
#define ENOTSOCK    88  /* Socket operation on non-socket */
#define EDESTADDRREQ    89  /* Destination address required */
#define EMSGSIZE    90  /* Message too long */
#define EPROTOTYPE  91  /* Protocol wrong type for socket */
#define ENOPROTOOPT 92  /* Protocol not available */
#define EPROTONOSUPPORT 93  /* Protocol not supported */
#define ESOCKTNOSUPPORT 94  /* Socket type not supported */
#define EOPNOTSUPP  95  /* Operation not supported on transport endpoint */
#define EPFNOSUPPORT    96  /* Protocol family not supported */
#define EAFNOSUPPORT    97  /* Address family not supported by protocol */
#define EADDRINUSE  98  /* Address already in use */
#define EADDRNOTAVAIL   99  /* Cannot assign requested address */
#define ENETDOWN    100 /* Network is down */
#define ENETUNREACH 101 /* Network is unreachable */
#define ENETRESET   102 /* Network dropped connection because of reset */
#define ECONNABORTED    103 /* Software caused connection abort */
#define ECONNRESET  104 /* Connection reset by peer */
#define ENOBUFS     105 /* No buffer space available */
#define EISCONN     106 /* Transport endpoint is already connected */
#define ENOTCONN    107 /* Transport endpoint is not connected */
#define ESHUTDOWN   108 /* Cannot send after transport endpoint shutdown */
#define ETOOMANYREFS    109 /* Too many references: cannot splice */
#define ETIMEDOUT   110 /* Connection timed out */
#define ECONNREFUSED    111 /* Connection refused */
#define EHOSTDOWN   112 /* Host is down */
#define EHOSTUNREACH    113 /* No route to host */
#define EALREADY    114 /* Operation already in progress */
#define EINPROGRESS 115 /* Operation now in progress */
#define ESTALE      116 /* Stale NFS file handle */
#define EUCLEAN     117 /* Structure needs cleaning */
#define ENOTNAM     118 /* Not a XENIX named type file */
#define ENAVAIL     119 /* No XENIX semaphores available */
#define EISNAM      120 /* Is a named type file */
#define EREMOTEIO   121 /* Remote I/O error */
#define EDQUOT      122 /* Quota exceeded */

#define ENOMEDIUM   123 /* No medium found */
#define EMEDIUMTYPE 124 /* Wrong medium type */
#define ECANCELED   125 /* Operation Canceled */
#define ENOKEY      126 /* Required key not available */
#define EKEYEXPIRED 127 /* Key has expired */
#define EKEYREVOKED 128 /* Key has been revoked */
#define EKEYREJECTED    129 /* Key was rejected by service */

/* for robust mutexes */
#define EOWNERDEAD  130 /* Owner died */
#define ENOTRECOVERABLE 131 /* State not recoverable */

#define ERFKILL     132 /* Operation not possible due to RF-kill */

#define EHWPOISON   133 /* Memory page has hardware error */

#endif

Source: http://www.xinotes.net/notes/note/1793/

fatal error LNK1120: 1 unresolved externals

Experiment error note.

OpenGL.obj : error LNK2001: unresolved external symbol “public: __thiscall CGLFont::CGLFont(void)” (??0CGLFont@@QAE@XZ)
Exe: Fatal Error lnK1120:1 Unresolved Externals

Error message: Unresolvable external symbol “public: Thiscall CGLFont::CGLFont(void)” code refers to content (such as functions, variables, or labels) that the linker cannot find in the library and object files.
some people on the Internet say there are two possible reasons: “1. The program lacks lib files in project -> 2. Add the required lib file to the link in setting.”

“2. In a program, functions are only declared undefined, and this problem is most likely caused by misspelling the name of the function when defining the function.”
http://wenda.tianya.cn/question/3e750b81a3b043cf

Fatal error C1010: unexpected end of file encountered while looking for precompiled header. Did you forget to add “ා include” StdAfx. H to the source

Fatal Error C1010: Unexpected end of file encountered while looking for precompiled headers. Did you forget to add “#include” stdafx.h “to the source?
Error analysis:
this error occurs because the compiler is looking for a precompiled indicator header (default #include “stdafx.h”) when the file does not end as expected. The header file “stdafx.h” with precompiled instructions was not found.
(because the project of every CPP file attributes the default is to use the precompiled header (/ YU), but add a third party documents not # include “stdafx. H” precompiled directives, so the compiler in the CPP file until the end did not find it)
my this problem occurred in, by means of adding files to add to existing within the MFC one big tuo. H and. CPP file. These.h and.cpp files belong to the standard C++ open source code category and have no deeper relationship with MFC.
Solution: a.

1) in the solution explorer, right-click on the corresponding. CPP file, click “properties”
2) on the left side in the configuration properties, PM on “C/C + +”, click “precompiled header”
3) changes on the right side of the first line of “create/use precompiled header,” option from the “use precompiled header (/ Yu)” to “do not use the precompiled header”
4) note:
2.
(not recommended)
1) in the solution, right click on the project, click on properties
2) in the configuration properties -> c/c++ -> Changing “use the precompiled header (/YU)” to “do not apply the precompiled header”
in the precompiled header will make every compilation process very slow
Refer to the blog: http://blog.csdn.net/zrm_1995/article/details/68490749?locationNum=2& fps=1

Analysis of compilation errors of “error conflicting types for function”

When you’re compiling a C program using GCC, you sometimes run into a compilation error that says “error: conflicting Types for ‘function.'” The definition of a function is not consistent with the declaration.

(a) First, let’s take a look at an example where the definition and declaration of a function are inconsistent:

#include <stdio.h>

int func(int a);

int func(void) {
    return 0;
}

int main(void) {

    func();

    return 0;
}

Compiler:

gcc -g -o a a.c
a.c:5:5: error: conflicting types for ‘func’
 int func(void) {
     ^
a.c:3:5: note: previous declaration of ‘func’ was here
 int func(int a);

You can see that this error occurs at compile time because the declaration and definition of “func” are inconsistent (one with arguments and one without).
(b) Recently, when I ported some code to JFFS2, this error also occurred at compile time. But I found that the declaration and definition of the function in the header file were exactly the same, which surprised me. The conclusion is that the function parameter type is defined after the function is declared. The simplified code is as follows:

#include <stdio.h>

void func(struct A *A);

struct A {
        int a;
};

void func(struct A *A)
{
        printf("%d", A->a);
}

int main(void) {
        // your code goes here
        struct A a = {4};
        func(&a);
        return 0;
}

Where the definition of “structure A” is placed after the “func” function declaration, and the func function argument is the “structure A*” type. The compilation results are as follows:

gcc -g -o a a.c
a.c:3:18: warning: ‘struct A’ declared inside parameter list
 void func(struct A *A);
                  ^
a.c:3:18: warning: its scope is only this definition or declaration, which is probably not what you want
a.c:9:6: error: conflicting types for ‘func’
 void func(struct A *A)
      ^
a.c:3:6: note: previous declaration of ‘func’ was here
 void func(struct A *A);
      ^

You can also see the output error “error: Conflicting Types for ‘func'”. Perhaps a compilation warning is a clue.

Possible causes of [errror] ID returned 1 exit status error in dev C + + Programmer

Yesterday, when I was debugging C program, I made this mistake, and my mentality was broken. But fortunately through their own a period of a period of program debugging, finally or to find the error. I will write it down, and I hope that the same small white encountered such mistakes, in time to find.
When the following error occurs:

There may be several reasons:
1. Library function spelling error. Such as: printf, Scanf, etc
2. The function name defined was misspelled during the call
3, when the execution of the function may function body itself
4. I haven’t noticed (maybe I haven’t come across yet) that other people say there are programs running.

That’s all I’ve found so far in writing C programs, and I hope it helps you

Error c2137 of C language: empty character constant (Fixed)

Write a function that removes all whitespace from a string
Error analysis:
This is because when the character constant is a space character, you cannot enter only two single quotes, you must also enter a space between the two single quotes; Otherwise, an error will be reported when compiling, prompting null characters, as shown in the figure:

After consulting materials, it is not allowed to put nothing between two single quotation marks. After modification, it is shown in the following figure:

Code:

#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include <stdlib.h>
void fun (char *str)
{
    int i=0;
    char *p=str;
    while(*p){
        if(*p!=' '){     //Del Space key
            str[i++]=*p;
        }
        p++;
    }
    str[i]='\0';        //add Terminator
}
main()
{
  char str[81];
  char Msg[]="Input a string:";
  int n;
  FILE *out;
  printf(Msg);
  gets(str);
  puts(str);
  fun(str); 
  printf("*** str: %s\n",str); 
  /******************************/
  out=fopen("out.dat","w");
  fun(Msg);
  fprintf(out,"%s",Msg);
  fclose(out);
  /******************************/
}

 

error: `cout’ was not declared in this scope

Error compiling C++ using GCC under Linux:

#include< iostream>
int main()
{
cout < < “Hello World!” < < endl;
return 0;
}

compiler error:
$g++ s.> s.bbpp: In function ‘int main(int, char**)’:
The



s.cpp:12: error: `cout’ was not declared in this scope

s.cpp:12: error: `endl’ was not declared in this scope

The reason:

C++ 1998 requires cout and endl to be called using ‘STD ::cout’ and’ STD ::endl’ formats, or using namespace STD;

Revised:

#include< iostream>

int main()

{
std::cout < < “Hello World!” < < std::endl;
return 0;
}

or

#include< iostream>

using namespace std;

int main(int argc, char *argv[])

{

cout < < “hello world” < < endl;
return 0;
}

Compile through.