Mixing c++ and c code during compilation

TL;DR: use extern "C" for calling C++ functions from C code and compile using gcc -lstdc++

Suppose that you need to use some C++ code in your C environment and translating this legacy code is not an option. That is, C is your principal language and you need some libraries in C++. In this post, I will show you how to compile C++ and C sources togheter You can use both languages and link the object files in a single executable. Consider the following example with three source files:

| legacy_code.h
| Interface between legacy code (c++) and an external (c) program.
| If your program need c++ libraries, such as iostream, include them in legacy_code.cpp
| Things to consider:
| 1) Note that both c and c++ programs could read this file, thus this file need to be 'readable' or compatible with respect to both languages.
| For that reason, dont put things like 
|   #include <iostream>  
|   using namespace std;
|   cout << var;
| 2) Put the following block before each function header
| #ifdef __cplusplus
|   extern "C"
| #endif


// printer for int
#ifdef __cplusplus
extern "C"
void print_int (int a);

// printer for double
#ifdef __cplusplus
extern "C"
void print_double(double a);

| legacy_code.cpp
| All your legacy code (c++ functions) are here. 
| Two things to notice in this file
| 1) The header of every function must be accessible from c, that is
|   void print_vector ( int *v )        it is OK
|   void print_vector ( vector<int> v ) NOT OK
| 2) If you need some c++ library, such as iostream, put it here, not in legacy_code.h

// -- c++ libraries here --
#include <iostream>
// -- end c++ libreries --

#include "legacy_code.h"

void print_int (int a) {
    std::cout << "integer " << a << std::endl;   

void print_double(double a) {
    std::cout << "double " << a << std::endl;    

| main.c
| This code will use c++ functions trough the legacy_code.h interface.
| how to compile (note the -lstdc++ flag)
| $ g++ -c legacy_code.cpp
| $ gcc -c main.c 
| $ gcc legacy_code.o main.o -o program -lstdc++

#include <stdio.h>
#include "legacy_code.h"

int main () {
    // we are using c++ functions right here
    return 0;   

# makefile
	g++ -c legacy_code.cpp
	gcc -c main.c 
	gcc legacy_code.o main.o -o program.out -lstdc++
	rm *.o *.out

Finally, compile these files using make or the following commands:

g++ -c legacy_code.cpp
gcc -c main.c
gcc legacy_code.o main.o -o program -lstdc++


how to compile the C++ WFG benchmark

TL;DR: you need to add a makefile!

WFG (Walking Fish Group) benchmark is a set of testing problems. Such a benchmark has been developed in C++ and can be downloaded here. However, compiling it can be problematic, at least for me. If you are facing the same issue, unzip WFG_v2006.03.28.zip, copy this snippet and save it as makefile inside WFG_v2006.03.28:

# compiler

# flags
CFLAGS=-c -Wall -g

all: main 

main:         main.o exampleproblems.o exampleshapes.o exampletransitions.o frameworkfunctions.o misc.o shapefunctions.o transfunctions.o
	$(CC) -g  main.o exampleproblems.o exampleshapes.o exampletransitions.o frameworkfunctions.o misc.o shapefunctions.o transfunctions.o -lm -o main.out

# -------------- dependencies ------------

	$(CC) $(CFLAGS) main.cpp

	$(CC) $(CFLAGS) Toolkit/ExampleProblems.cpp -o exampleproblems.o
	$(CC) $(CFLAGS) Toolkit/ExampleShapes.cpp -o exampleshapes.o
	$(CC) $(CFLAGS) Toolkit/ExampleTransitions.cpp -o exampletransitions.o
	$(CC) $(CFLAGS) Toolkit/FrameworkFunctions.cpp -o frameworkfunctions.o
	$(CC) $(CFLAGS) Toolkit/Misc.cpp -o misc.o
	$(CC) $(CFLAGS) Toolkit/ShapeFunctions.cpp -o shapefunctions.o
	$(CC) $(CFLAGS) Toolkit/TransFunctions.cpp -o transfunctions.o

# -------------- clean ------------

	rm *.o *.out

Your WFG directory should look like this:

auraham@roku:~/Desktop/zips/WFG_v2006.03.28$ ls
CHANGE_LOG.txt main.cpp makefile README.txt Toolkit

Then, execute the following command:

auraham@roku:~/Desktop/zips/WFG_v2006.03.28$ make

g++ -c -Wall -g main.cpp
g++ -c -Wall -g Toolkit/ExampleProblems.cpp -o exampleproblems.o
g++ -c -Wall -g Toolkit/ExampleShapes.cpp -o exampleshapes.o
g++ -c -Wall -g Toolkit/ExampleTransitions.cpp -o exampletransitions.o
g++ -c -Wall -g Toolkit/FrameworkFunctions.cpp -o frameworkfunctions.o
g++ -c -Wall -g Toolkit/Misc.cpp -o misc.o
g++ -c -Wall -g Toolkit/ShapeFunctions.cpp -o shapefunctions.o
g++ -c -Wall -g Toolkit/TransFunctions.cpp -o transfunctions.o
g++ -g main.o exampleproblems.o exampleshapes.o exampletransitions.o frameworkfunctions.o misc.o shapefunctions.o transfunctions.o -lm -o main.out

Now, you are ready to run the test program:

auraham@roku:~/Desktop/zips/WFG_v2006.03.28$ ./main.out WFG1

And that’s all!

Things to install on your fresh laptop

sudo apt-get install texlive-lang-spanish

sudo apt-get install python-setuptools

sudo apt-get install aspell aspell-es



inkscape + latex support (tex text plugin)
Download the latest version of textext from here. At the time of this writting, the latest version is textext-0.4.4.tar.gz.

Extract the content:

auraham@roku:~/Downloads$ tar xzvf textext-0.4.4.tar.gz

Copy these files to inkscape extensions directory:
auraham@roku:~/Downloads$ cp textext.* ~/.config/inkscape/extensions/

Install pstoedit:

auraham@roku:~/Downloads$ sudo apt-get install pstoedit

Restart inkscape. Textext will appear in the menu Extension/Tex Text. Before using this option, you need to save this ini template (template.ini or whatever you want):


Now, run tex text plugin. In the Preamble file option, select template.ini. Then, you will be able to write equations using latex syntax.


my touhpad is too slow (not so sensitive)!

tl;dr: use synclient to setup your touchpad instead of Mouse & Touchpad options on System Settings.

After installing Ubuntu 15.04 on my new HP 240, my first reaction was “well, my touchpad sucks, it is too slow”. But, what I tried to say was “it is not sensitive enough”. I use my touchpad (and probably you too with the tip of my finger. But, in order to use my new laptop, I need to use almost my whole fingertip! So, after googling for a while, I found this post with the solution: synclient. This utility enable us to configure our touchpad in a simple way. There is a lot of options to set up, but in order to solve this problem, you only need to change FingerLow and FingerHigh. To know what is your current configuration, execute:

synclient | grep FingerLow
synclient | grep FingerHigh

In order to change these values, execute:

synclient FingerLow=8 && synclient FingerHigh=10

Higher values implies less sensitivity. Finally, to make this change persisntent after rebooting, do the following:

cd /usr/share/X11/xorg.conf.d/
sudo cp 50-synaptics.conf 50-synaptics.conf.bak
sudo vim 50-synaptics.conf

And add “FingerLow” and “FingerHigh” in the corresponding block as follows:

Section "InputClass"
Identifier "touchpad catchall"
Driver "synaptics"
MatchIsTouchpad "on"
MatchDevicePath "/dev/input/event*"
# added
# http://ianrenton.com/guides/howto-linux-and-synaptics-touchpads-for-little-fingers/
Option "FingerLow" "8"
Option "FingerHigh" "10"

It is possible that the file


does not even exist on your computer. In this case, you can create from scratch. More info on Ian’s blog.

undefinedresource on ghostscript

TL;DR: Use ghostscript 9.05 instead of newer versions to use dvipdf

Today, I was trying to compile a DVI file with dvipdf, but I get this error:

Error: /undefinedresource in resourcestatus
Operand stack:
false ct_StyleDicts --dict:0/4(L)-- Adobe-Japan1 --dict:0/4(L)-- Serif HeiseiMin-W3-83pv-RKSJ-H Font HeiseiMin-W3 CIDFont
Execution stack:
%interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push 1967 1 3 %oparray_pop 1966 1 3 %oparray_pop 1950 1 3 %oparray_pop 1836 1 3 %oparray_pop --nostringval-- %errorexec_pop .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- 1915 10 11 %oparray_pop
Dictionary stack:
--dict:1185/1684(ro)(G)-- --dict:0/20(G)-- --dict:85/200(L)-- --dict:124/300(L)-- --dict:60/200(L)-- --dict:57/75(L)-- --dict:2/10(L)-- --dict:39/70(L)-- --dict:0/4(L)-- --dict:0/4(L)-- --dict:20/27(ro)(G)-- --dict:20/26(ro)(G)--
Current allocation mode is local
Last OS error: No such file or directory
GPL Ghostscript 9.15: Unrecoverable error, exit code 1

After trying to figure out what just happen, I remember that this DVI was correctly renderred on other PC, using ghostscript (GS) 9.05. So, in order to solve this issue, you need to install a previous version of GS. In this case, I will use 9.05:

1. Download http://downloads.ghostscript.com/public/ghostscript-9.05.tar.gz
2. Extract and install

tar xzvf ghostscript-9.05.tar.gz
cd ghostscript-9.05
sudo make install

In order to check  the GS version, type man gs and go to the end of the man pages. You should see 9.05 in the bottom of the man pages.

As far as I know, this issue is due to the EPS images in your document. In my case, I have used two EPS formats (you can check it using file image.eps command on linux):

1 Fig3.eps: PostScript document text conforming DSC level 3.0, type EPS
2 Fig4.eps: DOS EPS Binary File Postscript starts at byte 32 length 255343 TIFF starts at byte 255375 length 27871

Previous versions of GS can handle both EPS formats. However, newer version can handle only the first format.

More info: