使用g ++ .cpp文件编译gcc .o文件

kes*_*ari 1 c c++ gcc g++ object-files

我有两个文件,print_permu.cgen_permu.cpp.我希望将print_permu.c文件编译成目标文件,然后使用目标文件进行编译gen_permu.cpp,其中包含对函数的调用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);
}
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

我尝试了以下命令以上述方式编译代码.

$ 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
Run Code Online (Sandbox Code Playgroud)

首先,我使用编译代码gcc,希望编译时目标文件兼容g++.那没用.所以,我试图g++单独编译这两个文件,但无济于事.

我该如何编译这段代码?"我错过任何选择吗?

使用gcc版本4.8.4(Ubuntu 4.8.4-2ubuntu1~14.04),Ubuntu 14.04,x86_64


另外,

我最初想要在__print_permu_recurse内部声明函数print_permu,这在gcc中是允许的(尽管不是C标准).我曾经遵循过类似的过程.由于这没有成功,'改变了代码,即使用C++也兼容.

Dev*_*lar 6

你的问题是这个结构gen_permu.cpp:

extern "C"
{
        #include"print_permu.c"
}
Run Code Online (Sandbox Code Playgroud)

您有一个源文件print_permu.c和一个源文件gen_permu.cpp,其中包括 print_permu.c.

编译print_permu.c为目标代码时,它包含源文件中的所有内容print_permu.c.

编译gen_permu.cpp为目标代码时,它包含源文件gen_permu.cpp 和源文件中的所有内容print_permu.c.

当您尝试将它们中的两个链接在一起时,会出现"多个定义"错误,因为所有print_permu.c内容也都定义在gen_permu.cpp,并且链接器决定使用哪个定义.


你可能打算做的是包括来自的声明print_permu.c.这不是通过包含整个源文件,而是通过编写文件来完成的print_permu.h:

// This is a "header guard". It avoids problems if the
// header is included more than once in a translation unit.
// The token used (here: PRINT_PERMU_H) is up to you, but
// should be sufficiently unique. All uppercase is a common
// convention.
#ifndef PRINT_PERMU_H
#define PRINT_PERMU_H

// This makes a C++ compiler aware that function declarations
// refer to *C* code, not C++ code. (The two languages generate
// different linker symbols.) A C compiler will just ignore this
// (as it should, since it does not need any special handling).
#ifdef __cplusplus
extern "C" {
#endif

// This just *declares* the *existence* of the function.
// The compiler can use this information (in gen_permu.cpp)
// to create a call-to-placeholder. The linker will then
// turn that into a call-to-function when you link the two
// object files together.
void print_permu(size_t *char_count);

// End of the C++ compatibility construct.    
#ifdef __cplusplus
}
#endif

// End of the header guard.
#endif
Run Code Online (Sandbox Code Playgroud)

然后,而不是顶部的构造,只需写

#include "print_permu.h"
Run Code Online (Sandbox Code Playgroud)

在你的C++文件(或C文件中,由于它无关紧要#ifdef __cplusplus).


您的源有许多其他问题不会导致立即失败,但如果它们成为编码习惯会带来问题:

保留标识符.

全局变量.

魔术数字.

没有评论,并假设ASCII.

假设一个ASCII-7字符集,我很确定你的代码用字符做了一些可疑的东西,如果1)文本包含öéß等国际字符,或者2)所讨论的系统对字符有非连续编码,则会失败(例如EBCDIC).

但是我很无聊,试图找出你的代码实际上要做什么,因为那里没有任何评论.(想象一下,我发布了没有评论的标题示例...)所以我不能给你提示如何改进,只是你应该.

我也看到一个for没有大括号({})的循环.;-)