Tag Archives: C/C++

DevC++ Error: [Error] Id returned 1 exit status [How to Solve]

DEVC + + reports an error [error] ID returned 1 exit status

Cause

The computer in the school computer room always compiles twice and always reports errors

Error reporting prompt

[Error] Id returned 1 exit status

Solution

Premises

First check whether the console window of your program is closed, and then recompile if there is an error. The following scheme is for the case where an error is still reported after closing the window.

Operation

Right click dev C + + icon -> Properties – > Compatibility > Check “run this program as an administrator” open dev C + + –> tools –> compilation options –> code generation/Optimization –> connector –> set “link objective C program” to yes, and switch the compiler in the upper right corner of the dev C + + main page to debug mode (for example, tdm-gcc 4.9.2 64 bit debug) If your computer has a restore card, please place the project and its files on an unprotected disk.

error: ‘CLOCK_MONOTONIC‘ undeclared (first use in this function)

  Error message:

/home/xx/test/main.c: In function ‘main’:
/home/xx/test/main.c:37:21: error: storage size of ‘start’ isn’t known
   37 |     struct timespec start, end; //nanoseconds
      |                     ^~~~~
/home/xx/test/main.c:37:28: error: storage size of ‘end’ isn’t known
   37 |     struct timespec start, end; //nanoseconds
      |                            ^~~
/home/xx/test/main.c:43:5: warning: implicit declaration of function ‘clock_gettime’ [-Wimplicit-function-declaration]
   43 |     clock_gettime(CLOCK_MONOTONIC, &start);
      |     ^~~~~~~~~~~~~
/home/xx/test/main.c:43:19: error: ‘CLOCK_MONOTONIC’ undeclared (first use in this function)
   43 |     clock_gettime(CLOCK_MONOTONIC, &start);
      |                   ^~~~~~~~~~~~~~~
/home/xx/test/main.c:43:19: note: each undeclared identifier is reported only once for each function it appears in
make[2]: *** [CMakeFiles/WBSM4.dir/build.make:63: CMakeFiles/xx.dir/test/main.c.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:78: CMakeFiles/xx.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

Solution:

Add a compiler to the cmakelists.txt file:  

add_compile_options(-D_POSIX_C_SOURCE=199309L) 

Solution source [copyright infringement and deletion]:

c++ – error: ‘CLOCK_ MONOTONIC’ undeclared (first use in this function) – Stack Overflow

error: ‘CLOCK_ Mononic ‘undeclared problem solving_ Whahu 1989 column – CSDN blog

How to Solve Fopen bus error

Bus error (core dumped) occurs in my program, and the error is locked in the fopen part of the code

void dipget(int icdp, int nti, float *dip, char * tanpath)
{
  FILE * dfile;
  char *filename;

  printf("Choose %d from %s\n",icdp, tanpath);
  sprintf(filename, "%s%d.x.dat",tanpath, icdp);
  printf("Open file: %s \n", filename);

  if((dfile = fopen(filename,"rb"))==NULL) // Bus error
  {
    printf("\n Open %s File error\n",filename);
    exit(EXIT_FAILURE);
  }
  fread(dip, sizeof(float), nti, dfile);
  fclose(dfile);
}

Here, you only need to make one step of modification to solve the problem. The reason is very simple. Before using sprintf, you need to allocate a buffer space to it

  char *filename;
  Change to
  char filename[200];

error: initializer element is not constant [How to Solve]

1. Background

C language compilation error: error: initializer element is not constant. The error code is as follows:

    char *info = (char *)malloc(len);
    static char *info_t = info;

The above error is caused by trying to initialize a static variable with the memory pointer allocated by malloc.

2. Error reason

Static variable is a global variable. The value of the global variable cannot be determined at compile time, but at run time (compilation principle). Therefore, the global variable cannot be initialized directly with the return value of malloc at the time of declaration. Replace with the following:

    char *info = (char *)malloc(len);
    static char *info_t; 
    info_t = info;

Compilation passed.

C Language error: two or more data types in declaration specifiers

Libmemcached encountered an error during compilation:

./config.h:632:15: error: two or more data types in declaration specifiers
 #define off_t long int
               ^
./config.h:632:20: error: two or more data types in declaration specifiers
 #define off_t long int
                    ^
./config.h:658:17: error: two or more data types in declaration specifiers
 #define ssize_t int

Since config. H is generated through the automake tool, I don’t know where to find the problem for a while
first write a try. C file:

#define off_t long int
#define ssize_it int
int main() {
	return 1;
}

Can be compiled
multiple attempts have found that it is related to the order of #define. If #define off_ T long int in #define < stdxx.h> Before, an error will be reported, otherwise no error will be reported
via G + + - E - P try. C > XX , you can see:

#define long int __off_t
#define __off_t off_t

If#define off_t long intis ahead of stdxx.h,then use long int to replace off_t:

#define long int __off_t
#define __off_t long int

A loop occurs, so an error will be reported
and if #define off_ T long int after stdxx. H
because you already have #typedef__ off_ t off_ T without execution

In addition, it is found that #include “A.H” can also be used as #include & lt; a.h>, Previously, it was thought that local. H files could only be imported with “” instead of & lt& gt;, Now it is found that with GCC - I. , the directory with – I and include can be used with & lt& gt; Import local H files.

In addition, G + + can get preprocessed output in – E – P X.C, that is, a. CC file. The extern "C" {...} requires that the precompiled output must be compiled by G + +.

Error: expected class name before ‘{‘ token

The error of C + + compiler is as follows:

error: expected class-name before ‘{’ token

When deriving a class, you need to confirm whether it contains the header file of the base class.

If the header file of the base class is included, an error is still reported. As mentioned above, check the header file contained in the base class header file.
For example, all the information about an error is as follows:

aarch64-himix100-linux-g++ -c ./src/base.cpp ./src/host.cpp ./src/main_test.cpp ./src/mcu.cpp ./src/uart1.cpp 
In file included from ./src/../include/main_test.h:6:0,
                 from ./src/../include/base.h:6,
                 from ./src/base.cpp:1:
./src/../include/uart1.h:10:1: error: expected class-name before ‘{’ token
 {
 ^
make: *** [main_test.o] Error 1

There are base classes in the base. H file. Uart1. H is the class to be derived. But look at all the above error information, it appears from the sixth line of base. H. Find line 6, which is the header file of the main function (test program).
Put the header file in line 6 base.cpp It’s OK.

In fact, the above situation should not appear in base. H and base.cpp The header file of the test file should not appear in.

Put the header files (such as # include & lt; pthread. H & gt;) in the corresponding header files (such as base. H).

When testing, you only need to include the base. H file to test the functions in the base.

Solve the problem of error: cannot pass objects of non trivially copyable type ‘STD:: String’ in C / C + +

catalog

1. Problem description 2. Cause analysis 3. Solution

1. Problem description

When running the program, the compiler reports an error:

error: cannot pass objects of non-trivially-copyable type ‘std::string {aka struct std::basic_ string}’ through ‘…’ |

2. Cause analysis

The error is displayed on this line:

printf("%c %s %lld %lld\n", p, edges, ver, edge);

Where edges is of string type.

Grammar:

const char *c_ str();c_ Str() function returns a pointer to a normal C string, the content of which is the same as that of this string string. In order to be compatible with C, there is no string type in C, so it must pass the member function C of string class object_ Str () converts a string object to a string style in C.

3. Solutions

In use, add C_ str();

printf("%c %s %lld %lld\n", p, edges.c_str(), ver, edge);

Error: transfer of control bypasses initialization of: variable XXX solution

Error: transfer of control bypasses initialization of: variable XXX

The nature of the problem, causes and Solutions

The nature of the problem

The code may skip the initialization of some variables, which makes the program access the uninitialized variables and crash.

Causes

I encountered this problem when porting the code from the vs compiling environment of windows to the G + + of Linux (actually compiling CUDA C + + code with nvcc, but the nvcc background also calls G + + to compile C / C + + part of the code). Later, it was found that in vs environment, it was just a warning, but in G + +, it was an error, so this problem must be solved.

terms of settlement

    whether the variables in the analysis code are initialized, whether the variables are declared after the goto statement in the analysis code, and if so, move the variable declaration to the front of goto .
// error
goto SomeWhere;
int var = 10;
// right
int ver = 10;
goto SomeWhere;
    analyze whether or switch statements appear in the code, because switch statements are essentially implemented with goto , so the above problems may also exist. In addition to reference 2, write the variable declaration before switch, add curly brackets to each branch of switch , or change the switch statement to if / else .
// error
switch (choice)
{
    case 1:
        // do something
        break;
    case 2: 
        // do something
}
// right
switch (choice)
{
    case 1:
    {
        // do something
        break;
    }
    case 2: 
    {
        // do something
    }
}
// or
if(choice == 1)
{
	// do something
}
else if(choice == 2)
{
	// do something
}

Compiler error message: cs1056: unexpected character handling

Error generation:
always returns an error when compiling a pre-existing program code using VS2015. strange In some added to the end of the class file some [] [] [] [], prompt Unexpected character, ‘application error.

A compilation error as shown in the title was caused by a special full-corner character in C# code. > special full-corner characters are not displayed and saved in UTF-8 format, and the compiler cannot correctly recognize such characters, resulting in compilation errors.

solution:
set the file open mode of VS 2015, make it contains a file with the Angle of all special characters use utf-8 format to open.
>
1) In Solution Explorer, select the file that needs special opening. 2) Right-click the mouse button to pop up the menu, select “Open Mode”, and use “Charp Editor with Encoding” to open.

Reproduced in: https://blog.51cto.com/studybao/2048899

Three methods of referencing DLL in Visual Studio 2015

Copy the DLL file to the directory where the executable is located
 
2. Transfer engineering attributes ->; Configure properties ->; Debugging – & gt; Change the working directory to the directory where the DLL files reside
 
3. Transfer engineering attributes ->; Configure properties ->; Debugging – & gt; Set the environment to the Path= DLL directory, for example: Path=$(SolutionDir)bin
 

Reproduced in: https://www.cnblogs.com/buyishi/p/10236780.html