Naming Convention Requirement

Both the calling program and the called subprogram must agree on the names of identifiers. Identifiers can refer to subprograms (functions, procedures, and subroutines) or to variables that have a public or global scope. Each language alters the names of identifiers.

The term “naming convention” refers to the way a compiler alters the name of the routine before placing it in an object file. Languages may alter the identifier names differently. You can choose between several naming conventions to ensure that the names in the calling program agree with those in the called program. If the names of called routines are stored differently in each object file, the linker will not be able to find a match. It will instead report unresolved external references.

Microsoft compilers place machine code into object files; they also place the names of all publicly accessed routines and variables in object files. The linker can then compare the name of a routine called in one module with the name of a routine defined in another module, and recognize a match. Names are stored in the ASCII (American Standard Code for Information Interchange) character set.

Summary: Some languages translate names to uppercase.

BASIC, FORTRAN, and Pascal use similar naming conventions. They translate each letter to uppercase. BASIC type declaration characters (%, &, !, #, $) are dropped.

Each language recognizes a different number of characters. FORTRAN recognizes the first 31 characters of any name (unless identifier names are truncated), Pascal the first 8, and BASIC the first 40. If a name is longer than the language will recognize, additional characters are simply not placed in the object file.

Note:

Versions of Microsoft FORTRAN previous to version 5.0 truncated identifiers to six characters. As of version 5.0, FORTRAN retains up to 31 characters of significance unless you use the /4Yt option.

Summary: C and C++ are case-sensitive languages.

The C compiler does not translate any letters to uppercase. It inserts a leading underscore (_) in front of the name of each routine. The C compiler recognizes the first 31 characters of a name (or 32 including the underscore). You can change the number of characters it recognizes with the /H option; see Chapter 13, “CL Command Reference,” in the Environment and Tools manual for more information.

The C++ compiler decorates identifier names to retain type information through the linking process. The C++ compiler recognizes the first 247 characters of a name.

Differences in naming conventions are dealt with automatically by mixed-language keywords, as long as you follow two rules:

If you use any FORTRAN routines that were compiled with the /4Yt command-line option or with the $TRUNCATE metacommand enabled, make all names six characters or less. Make all names 6 characters or less when using FORTRAN routines compiled with versions of the FORTRAN compiler prior to 5.0.

Do not use the /NOIGNORECASE linker option (which causes the linker to treat identifiers in a case-sensitive manner). With C or C++ modules, this means that you must be careful not to rely upon differences between uppercase and lowercase letters when programming.

CL automatically uses the /NOIGNORECASE option when linking. To solve the problems created by this behavior, either link separately with the LINK utility, or use all lowercase letters in your C or C++ function names and public variables (global variables that are not declared as static).

Note:

If you use the command-line option /Gc (generate Pascal-style function calls) when you compile, or if you declare a function or variable with the __pascal keyword, the compiler will translate your identifiers to uppercase.

Figure 11.2 illustrates a complete mixed-language development example, showing how naming conventions enter into the process.

In Figure 11.2, note that the BASIC compiler inserts a leading underscore in front of Prn as it places the name into the object file, because the CDECL keyword directs the BASIC compiler to use the C naming convention. BASIC will also convert all letters to lowercase when this keyword is used. (Converting letters to lowercase is not part of the C naming convention; however, it is consistent with the programming style of many C programs.)