簡體   English   中英

使用g ++ .cpp文件編譯gcc .o文件

[英]Compiling gcc .o files with g++ .cpp files

我有兩個文件print_permu.cgen_permu.cpp 我希望將print_permu.c文件編譯為目標文件,然后使用該目標文件編譯gen_permu.cpp ,該文件包含對print_permu.c的函數的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_recurse內部功能print_permu ,這將在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 )。


您的源還存在其他各種問題,這些問題不會立即導致失敗,但是如果成為編碼習慣,則會帶來問題:

保留的標識符

全局變量

魔術數字

沒有注釋 ,並假定為ASCII

我非常確定您的代碼會使用ASCII-7字符集對字符進行處理,如果1)文本包含öéß等國際字符,或者2)所討論的系統對字符采用非連續編碼(例如EBCDIC )。

但是我很無聊,無法弄清楚您的代碼實際上打算做什么,因為其中的注釋為零。 (想象一下,我發布的標題示例中沒有注釋...)所以我不能給您提示如何改進的建議,只是您應該這樣做。

我還看到了不帶大括號( {} )的for循環。 ;-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM