Deep understanding of compiler-related common errors in C language _c language

Source: Internet
Author: User
1./usr/lib/gcc/i686-linux-gnu/4.6/. /.. /.. /i386-linux-gnu/crt1.o:in function ' _start ':
(. text+0x18): Undefined reference to ' main '
COLLECT2:LD return 1
Reason:no main function in source file
2. To get compile options-i and-l
Pkg-config Lib
E.g:pkg-config--cflags--libs dbus-1 glib-2.0 dbus-glib-1
Gcc-o send-sms send-sms.c pkg-config--cflags--libs dbus-1 glib-2.0 '
3. How to let Pkg-config find their own written library
There is a file libxxx.pc.in in the library that defines where the header files it provides, what their libraries are linked to, where the libraries are, and, of course, this is the information that the library is installing to the system, in other words, it may be meaningless for the compilation environment.
Copy Code code as follows:

prefix= @PREFIX @
Exec_prefix=${prefix}
Libdir=${exec_prefix}/lib
Includedir=${prefix}/include
Name:library Name
Description:description goes here
requires:glib-2.0 gobject-2.0
version:0.1
Libs:-l${libdir}-llibrary_name
Cflags:-i${includedir}/some_sub_dir

This file defines all the information for this library after installation, and Pkg-config reads this information.
4. Forward declaration and incomplete type
This type of error is usually due to the specific use of a particular type, but it is only declared and undefined (at the time of use). For example, a head file has the following statement:
Copy Code code as follows:

#ifndef __point_h
#define__POINT_H
typedef struct _POINT point;
#endif

If you have a file that contains this header file, you can use point to declare:
1. If you declare a function as a formal parameter, void Print_point (point P), note that when declaring a function, rather than defining a function
2). Declaration pointer: Point *p;
But you can't use point to define variables,
1). If you define a variable, point p;
2. The formal parameters when the function is defined, void Print_point (point P) {...}
3). Or for its pointer to apply within space, point *point = (point *) calloc (1, sizeof (point));
A compile error for the incomplete type is reported. Because this time need notification the memory size and the concrete definition form, but the header file does not give the concrete definition, therefore the compiler does not know this type to need the memory, therefore compiles the error.
This is also true in C + +, in order to efficiency forward declaration, that is, before using a class, not specifying its class, but declaring an undefined class:
Class point;
Point A;
When you use Foward declaration, you can only use it to declare, not to use this type specifically.
So, if you want to use a type specifically, the header file it contains must have a specific definition of the type:
Copy Code code as follows:

#ifndef __point_h
#define __point_h
typedef struct _POINT point;
struct _point {
int x;
int y;
};
#endif
#include "Point.h"
Point *n = (point *) calloc (1, sizeof (point));
N->x = 1;
N->y = 2;
....

In fact, the reason is also very simple, when you need a type to declare a variable, do not need to allocate memory, do not have to operate it, naturally do not have to understand its specific type definition. But when you use, to allocate memory, you have to understand how the type is defined, otherwise these operations can not be completed, which naturally needs to know the specific definition of the type.
In fact, the purpose of declaring a type in a header file is to hide the information, that is, to not let the caller know what the specific definition of the type is, then you need to define that type as in java/c++.
1 Declare the type as the pointer type:
typedef struct point *point;
Otherwise, it is possible for the caller to define it.
2 It also means to encapsulate all the methods of this type in the corresponding source file of the header file so that there is no need for the outside world to understand how the type is defined. When it wants to operate, it only needs to invoke the encapsulated method.
A typical example:
Header file Point.h:
Copy Code code as follows:

#ifndef __point_h
#define __point_h
typedef struct _POINT *point;
Point Make_point ();
void Print_point (point point);
void Destroy_point (point P);
#endif

Implementing Source Files: point.c
Copy Code code as follows:

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "Point.h"
struct _point {
int x;
int y;
};
Point Make_point () {
Point point = (point) calloc (1, sizeof (struct _point));
point->x = 0;
point->y = 0;
return point;
}
void Print_point (point point) {
printf ("Point%d,%d\n", Point->x, Point->y);
}
void Destroy_point (point P) {
if (p = = NULL) {
printf ("Warning, destroying NULL object");
Return
}
Free (p);
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.