Tag Archives: C/C++

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 + +


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.


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.


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
    case 2: 
        // do something
// right
switch (choice)
    case 1:
        // do something
    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.

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

Configure OpenGL development environment (vs2015)

Configuring the OpenGL development environment (VS2015)
There are a lot of configuration VS + OpenGL tutorials online, for the configuration of the super treasure of the good and bad are mixed, mainly the official website did not give a good compiled library, many need to compile their own, so for beginners, it is difficult to configure successfully.
The following is a blog post that explains the configuration process and the rationale behind it
For beginners, everyone wants to be able to run the example given in the super bible. Here I directly give several twists and turns, all kinds of files generated after compilation, according to the following configuration can be run in VS2015 super treasure in the example, to achieve development.
I. Required allocation of resources

Resource download: http://download.csdn.net/download/ax_hacker/10107105
The zip package contains two folders, CJBD5_CODE and OpenGL. The former contains the sample program, and the latter is the three files required for configuration.

How do I configure the development environment, as I explained in a previous blog post
The following is the configuration process for OpenGL.
1, Copy the files in the DLL folder to C:\Windows\ Syswow64 and C:\Windows\System32
2. Create an empty project, add a source file, and configure the project properties
1) Put the include from the previous folder under the include directory

2) lib in the library directory

3) Linker ->; Input – & gt; Additional dependencies freeglutd.lib; GLTools.lib; freeglut.lib; glew32s.lib

A series of problems in configuring OpenGL development environment in vs2015

Since Windows 7, the OpenGL header file has been changed to the Windows SDK, so you can’t use it if you don’t install it.
more funny is, under the win10 vs2015 recompense Windows SDK installation failure, the typical pit themselves.
> VS05 and VC6 OpenGL headers are directly included in the development environment directory.

Okay, let’s get started.
: \Program Files (x86)\Windows Kits\8.1\Include\um\ GL C> Program Files (x86)\Windows Kits\8.1\Include\um\ GL
The downloaded from https://www.opengl.org/resources/libraries/glut/ glutdlls37beta. Zip , and will be. H in the directory. At the same time will glut32. Lib, glut. Lib respectively on the
C: \ Program Files \ Windows (x86) Kits \ 8.1 \ lib \ winv6.3 \ um \ x86 , C: \ Program Files \ Windows (x86) Kits \ 8.1 \ lib \ winv6.3 \ um \ x64 directory.
As for the DLL, as long as you have the graphics driver installed correctly, you can ignore it (if not, put it in the system directory or the directory where the current executable is located).
note: the above list I found from the vs2015 project configuration, there is a $(WindowsSDK_IncludePath) of environment variables, you can find winsdk related parameters from the inside.
So you can compile OpenGL programs normally.
If you need to rely on OpenGL extensions provided by a particular vendor, you will need to go to the vendor's website to download the development kit separately, such as Intel, Nvidia, AMD, IBM, etc.
The original address
Today debug OpenGl source program, compiled through, but a running tips, computer lost glut32. DLL files, not depressed, search under the Internet provide most of the practice are more, copying the file to download this file to C: \ WINDOWS \ system in 32, but did so after found that still doesn't work, after a long afternoon, suddenly realized that may be about my new WINDOWS 7 system, but also for loading is 64 - bit, then, Try to copy this glut32.dll to your SysWOW64 folder in C:\ Windows, and it will all be OK. Here's a special note to remind everyone who installed Windows 7 and VC ++ 6.0 to pay attention to it.
PS: When you first start to contact OpenGL, you will inevitably encounter some problems, these problems may have nothing to do with the program, just some compile environment Settings and the installation of header files, special sorting, as follows:
PS: When you first start to contact OpenGL, you will inevitably encounter some problems, these problems may have nothing to do with the program, just some compile environment Settings and the installation of header files, special sorting, as follows:
(1) Copy gult32.dll, Glut. DLL to Windows system system32(if Windows 7 is a 64-bit operating system, it is in C:\ Windows SysWOW64 file)
(2) Copy gult32.lib, Glut. lib to VC lib directory
(3) Copy ULT. H to include\GL of VC
The original address
Introduction to OpenGL

C / C + + cannot open the source file directory, the header file is not imported, the solution is as follows

The two methods
Configure the various dependencies
1. Project, Properties, C/C++, Additional Includes Directory: Fill in Additional Includes Directory “add. H file folder path semicolon interval multiple
3> Item, Property, Linker, Input, Additional Dependency: Fill in the name of the additional dependency library. Lib space or semicolon interval multiple
Two, the use of code directly import
Copy the required xxx.h file to the compile file
Copy the required copy of the xxx.lib library to the build

IntelliSense: unable to open source file ‘StdAfx. H’

-h /.cpp file: cannot open the source file “stdafx.h”, some methods found are not applicable, such as: set project properties ->; Configure properties ->; C/C++-> Precompiled header ->; Using (/Yu)/ creating (/Yc)/ not using precompiled headers does not work either way. Despite this error, the program was run successfully. The initialization and compilation runs seem to use different lookup paths, which is a problem when developed with a lower version of VS2005/VS2008 but opened with a higher version of VS2010. The solution is:
< wbr> < wbr> < wbr> < wbr> < wbr> < wbr> < wbr> < wbr> < wbr> < wbr> < wbr> Project Properties -& gt; Configure properties -& gt; C/C++-& gt; General – & amp; gt; Additional include directory -& gt; < strong> $(ProjectDir)< /strong> < /wbr> < /wbr> < /wbr> < /wbr> < /wbr> < /wbr> < /wbr> < /wbr> < /wbr> < /wbr> < /wbr>