c++ - 用 g++ .cpp 文件编译 gcc .o 文件

标签 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);
}

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;
}

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

$ 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

首先,我使用gcc编译代码,希望目标文件在使用g++编译时能够兼容。那没有用。因此,我尝试单独使用 g++ 编译这两个文件,但无济于事。

如何编译这段代码? '我是否遗漏了任何选项?

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


此外,

我最初想在 print_permu 中声明 __print_permu_recurse 函数,这在 gcc 中是允许的(尽管 C 标准不允许)。我遵循了类似的过程。由于这没有成功,'更改了代码以使其甚至与 C++ 兼容。

最佳答案

您的问题是 gen_permu.cpp 中的这个构造:

extern "C"
{
        #include"print_permu.c"
}

你有一个源文件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

然后,代替顶部的构造,只写

#include "print_permu.h"

在您的 C++ 文件中(或 C 文件,由于 #ifdef __cplusplus 并不重要)。


您的源代码还有其他各种问题,这些问题不会立即导致失败,但如果它们成为编码习惯就会带来问题:

Reserved identifiers .

Global variables .

Magic numbers .

无评论,并且假定为 ASCII

我很确定你的代码对字符做了一些可疑的事情,假设是 ASCII-7 字符集,并且如果 1) 文本包含像 öéß 这样的国际字符,或者 2) 有问题的系统有非连续编码,将会失败对于字符(例如 EBCDIC )。

但我对弄清楚您的代码的实际用途感到厌烦,因为其中的评论为零。 (想象一下,我已经发布了没有评论的标题示例......)所以我不能给你关于如何改进的提示,只是你应该。

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

关于c++ - 用 g++ .cpp 文件编译 gcc .o 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32075549/

相关文章:

C++ 绑定(bind)非静态成员函数

objective-c - cgo godefs 和 Objective-C

c++ - 将资源文件嵌入可执行文件的性能

c - 从输入参数声明自动变量的数组大小

无法使用表存储在 Azure 云辅助角色中加载文件或程序集 Microsoft.Data.OData Version=5.2.0.0 错误

c++ - 如何修复 g++ Undefined symbols for architecture x86_64 错误?

unix - ar 在现有的 .a 文件上?

c++ - tensorflow c_api.h 为什么GPU比CPU慢

c++ - 如何在不改变 QPlainTextEdit 类焦点的情况下检测文本的变化?

c++ - c模板函数参数类型的选择