How do you link a C++ program to C functions?

Answers were Sorted based on User's Feedback



How do you link a C++ program to C functions? ..

Answer / ravindranath m

The C++ compiler does something called as "name mangling"
for functions, while a C compiler does not. Name mangling
is a process wherein the name of the original function in a
c++ program gets changed to a new name via adding some
prefix and/or postfix to it.

As a result, a c program cannot find the required definition
when trying to link to a cpp object file.

This can be resolved by putting the following declaration in
a c++ header file that contains the cpp function declarations.
#ifdef __cplusplus
extern "C" {
#endif

// function declarations go here...
// ...

#ifdef __cplusplus
}
#endif

Is This Answer Correct ?    25 Yes 4 No

How do you link a C++ program to C functions? ..

Answer / guest

By using the keyword ?extern?

Is This Answer Correct ?    23 Yes 7 No

How do you link a C++ program to C functions? ..

Answer / nikhil upadhyay

By using the extern "C" linkage specification around the C function declarations.
Programmers should know about mangled function names and type-safe linkages. Then they should explain how the extern "C" linkage specification statement turns that feature off during compilation so that the linker properly links function calls to C functions. Another acceptable answer is "I don't know. We never had to do that." Merely describing what a linker does indicates that the programmer does not understand the issue that underlies the question.

Is This Answer Correct ?    0 Yes 0 No

How do you link a C++ program to C functions? ..

Answer / prabakaran

including the header file #include<stdio.h>

Is This Answer Correct ?    3 Yes 20 No

Post New Answer

More C++ General Interview Questions

Tell me an example where stacks are useful?

0 Answers  


Explain how to initialize a const member data.

0 Answers  


Why preincrement operator is faster than postincrement?

5 Answers  


If P is the population on the first day of the year, B is the birth rate, and D is the death rate, the estimated population at the end of the year is given by the formula: The population growth rate is given by the formula: B – D Write a program that prompts the user to enter the starting population, birth and death rates, and n, the number of years. The program should then calculate and print the estimated population after n years. Your program must have at least the following functions: 1. growthRate: This function takes its parameters the birth and death rates, and it returns the population growth rate. 2. estimatedPopulation: This function takes its parameters the current population, population growth rate, and n, the number of years. It returns the estimated population after n years Your program should not accept a negative birth rate, negative death rate, or a population less than 2.

1 Answers  


What is a string example?

0 Answers  






Difference between struct and class in terms of access modifier.

0 Answers  


Explain function overloading and operator overloading.

0 Answers  


Which operations are permitted on pointers?

0 Answers  


program in c++ to input digits and print in words

1 Answers   Microsoft,


What is "map" in STL?

2 Answers  


How do you define/declare constants in c++?

0 Answers  


What are the two types of comments, and how do they differ?

0 Answers  


Categories