C Linkage


Dialogue 1003: More Differences Between C and C++

Note: This dialogue is a work-in-progress!!! Don't put much stock into it until you see the checked box on the previous page. Also note that it is imaginary.



Introduction: The Biggest Misconception

simplicio:
salviati:
simplicio: Suppose I have a library, written in C. Let's keep it very simple, like this:
c_lib.c
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h&g;

#include "c_lib.h"

int set_temperature(struct city_data *example_city, float given_temp) {
example_city->temp = given_temp;
return 0;
}

simplicio: and I build this library with a C compiler, using command like gcc -o c_lib.so -shared lib/c_lib.c -g -Wall -Wextra -O0 -I include.
user.cpp
#include <iostream>
#include <list>

#include "c_lib.h"

class ReallyUseCpp {

public:

ReallyUseCpp() {

}

~ReallyUseCpp() {

}

private:
std::list cities;
};

int main(int argc, char **argv) {
char *example_name = "Baltimore";
struct city_data test_struct {
example_name,
39.00f
};
std::cout << "Program start" << std::endl;
std::cout << test_struct.temp << std::endl;
set_temperature(&test_struct, 46.00f);
std::cout << test_struct.temp << std::endl;
std::cout << "Program end" << std::endl;
return 0;
}

This discussion is imaginary! If you see a problem/error in it, then take it up with me.