I have two files, print_permu.c and gen_permu.cpp. I wish to compile print_permu.c file into an object file and then use the object file to compile gen_permu.cpp, which contains a call to a function in print_permu.c.
print_permu.c
#include<stdlib.h>
#include<stdio.h>
typedef char byte;
char *printable=NULL;
size_t pos,size,*char_cnt;
void __print_permu_recurse()
{
        if( pos==size )
        {
                printf("%s\n",printable);
                return;
        }
        byte iter = 25;
        while( iter>=0 )
        {
                if( char_cnt[iter] )
                {
                        printable[pos] = 'a'+iter;
                        --char_cnt[iter];
                        ++pos;
                        __print_permu_recurse();
                        --pos;
                        ++char_cnt[iter];
                }
                --iter;
        }
}
void print_permu(size_t *char_count)
{
        char_cnt = char_count;
        for(pos = 0,size = 0 ; pos<26 ; ++pos)
                size += char_count[pos];
        printable = (char*)malloc(sizeof(char)*(size+1));
        printable[size] = '\0';
        pos = 0;
        __print_permu_recurse();
        free(printable);
}
gen_permu.cpp
#include<iostream>
#include<string>
extern "C"
{
        #include"print_permu.c"
}
using namespace std;
int main()
{
        string str;
        size_t char_count[26]={},N,iter;
        cout << "string:"; cin >> str;
        N = str.size();
        for(iter=0;iter<N;++iter)
        {
                ++char_count[str[iter]-'a'];
        }
        print_permu(char_count);
        return 0;
}
I tried the following commands to compile the code in the said way.
$ gcc -c print_permu.c 
$ g++ print_permu.o gen_permu.cpp 
/tmp/ccQxAEea.o:(.bss+0x0): multiple definition of `printable'
print_permu.o:(.bss+0x0): first defined here
/tmp/ccQxAEea.o: In function `__print_permu_recurse':
gen_permu.cpp:(.text+0x0): multiple definition of `__print_permu_recurse'
print_permu.o:print_permu.c:(.text+0x0): first defined here
/tmp/ccQxAEea.o: In function `print_permu':
gen_permu.cpp:(.text+0xe0): multiple definition of `print_permu'
print_permu.o:print_permu.c:(.text+0xe9): first defined here
collect2: error: ld returned 1 exit status
$ g++ -c print_permu.c 
$ g++ print_permu.o gen_permu.cpp 
/tmp/ccPJA0kU.o:(.bss+0x0): multiple definition of `printable'
print_permu.o:(.bss+0x0): first defined here
/tmp/ccPJA0kU.o:(.bss+0x8): multiple definition of `pos'
print_permu.o:(.bss+0x8): first defined here
/tmp/ccPJA0kU.o:(.bss+0x10): multiple definition of `size'
print_permu.o:(.bss+0x10): first defined here
/tmp/ccPJA0kU.o:(.bss+0x18): multiple definition of `char_cnt'
print_permu.o:(.bss+0x18): first defined here
collect2: error: ld returned 1 exit status
First, I compiled code using gcc, hoping the object file would be compatible when compiling with g++. That didn't work. So, I tried to compile both the files using g++ alone, but to no avail.
How can I compile this code ? 'Am I missing any -options ?
Using gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04), Ubuntu 14.04, x86_64
Additionally,
I had initially wanted to declare the __print_permu_recurse function inside print_permu, which would be allowed in gcc (although not by C Standard). I had followed a similar process. Since that didn't work out, 'changed the code to be compatible even with C++.
 
     
    