multiple definitionというエラー

このQ&Aのポイント
  • Ubuntu LinuxのgccでC言語のプログラムを実行中に、multiple definitionというエラーが発生しました。
  • コンパイル時に追加したコードが原因で、実行ファイルが生成されない問題が発生しています。
  • const sig_code2_t PVC_TCP_SIG_CODE[100]の重複定義エラーが発生しており、コメントアウトすることで問題を解決できます。
回答を見る
  • ベストアンサー

multiple definitionというエラー

現在Ubuntu LinuxのgccでC言語のプログラムをやっています。 https://sourceforge.jp/projects/cc1101driver/scm/svn/blobs/2452/trunk/20140411/code.h このツリーのcode.hに ”const sig_code2_t PVC_TCP_SIG_CODE[100] = {” というコードを追加してmakeを行うと次のようなエラーが出て実行ファイルが出力されません。 (エラー内容) ---------------(中略)----------------- gcc -o exe main.o event.o env.o audio.o task.o comm.o menu_show_status.o mount.o net.o misc.o -lpthread event.o:(.rodata+0x0): multiple definition of `PVC_TCP_SIG_CODE' main.o:(.rodata+0x0): first defined here env.o:(.rodata+0x0): multiple definition of `PVC_TCP_SIG_CODE' main.o:(.rodata+0x0): first defined here audio.o:(.rodata+0x0): multiple definition of `PVC_TCP_SIG_CODE' main.o:(.rodata+0x0): first defined here task.o:(.rodata+0x0): multiple definition of `PVC_TCP_SIG_CODE' main.o:(.rodata+0x0): first defined here comm.o:(.rodata+0x0): multiple definition of `PVC_TCP_SIG_CODE' main.o:(.rodata+0x0): first defined here mount.o:(.rodata+0x0): multiple definition of `PVC_TCP_SIG_CODE' main.o:(.rodata+0x0): first defined here net.o:(.rodata+0x0): multiple definition of `PVC_TCP_SIG_CODE' main.o:(.rodata+0x0): first defined here misc.o:(.rodata+0x0): multiple definition of `PVC_TCP_SIG_CODE' main.o:(.rodata+0x0): first defined here collect2: error: ld returned 1 exit status make: *** [exe] Error 1 この const sig_code2_t PVC_TCP_SIG_CODE[100] = {をコメントアウト正常にビルドできるのですが、 この問題を解決するにはどうすれば良いのかご教示頂きますようお願い致します。

質問者が選んだベストアンサー

  • ベストアンサー
  • Wr5
  • ベストアンサー率53% (2177/4070)
回答No.1

お約束の「ヘッダに実体を定義してはいけない」ってヤツです。 code.hに記述した内容はall_includes.hに適用されます。 all_includes.h内で#include "code.h"していますからね。 で、all_includes.hの内容はそれぞれ audio.c、comm.c、env.c、event.c、main.c、misc.c、mount.c、net.c、task.cに適用されます。 つまり、code.hに記述した内容も audio.c、comm.c、env.c、event.c、main.c、misc.c、mount.c、net.c、task.cに適用されます。 それぞれのオブジェクトファイルにPVC_TCP_SIG_CODE[100]が存在します。 リンクする段階でどのオブジェクトファイルのPVC_TCP_SIG_CODE[100]を使用したらいいのでしょうか????? というエラーです。 extern宣言して、実体をどこかのソースファイルのみに記述する。 というのが一般的な対処方法です。 # static宣言にすればとりあえずリンクは通るかもしれませんが…無駄が出ますねぇ。 # 今回はconstなので、メモリに無駄が出るだけ…で済むかと思われますが。 http://d.hatena.ne.jp/saitodevel01/20110321/1300685958

techhouse
質問者

お礼

回答頂きありがとうございます。一応、エラーを回避することができました。とりあえずこれで進めて見ます。助かりました。

関連するQ&A

  • 【gcc・cygwin】multiple definitionエラーの解決法

    こんにちは。 C言語のプログラミングをしているのですが、 multiple definition of '関数名'と出てしまいます。 ・コンパイルするファイル program1.c(main文) program2.c(関数) program3.c(関数) すべてのソースファイルが、 header.hをincludeしています。 header.hではすべての関数のプロトタイプ宣言がされています。 program2.cに書いてある関数、test()はすべてのソースファイルで使用しています。 ・cygwin上で実行したコマンド gcc -Wall program1.c program2.c program3.c -o test ・エラーの文章 (パス省略)/ccst1v2u.o:program2.c:(.text+0x0): mult iple definition of `_test' (パス省略)/ccyUr2Ku.o:program1.c:(.text+0x0): first defi ned here このような事態に陥った場合の解決方法はどのようにすればよいでしょうか? 先輩から引き継いだ、もともと一つのソースファイルに書かれていた多数の関数を、利便性のために分割したところこのようなエラーが発生しました。 先輩から引き継いだもののため、本当の関数名など細かいことをこちらに書き込めないのですが、 出来る範囲で補足いたします。 どうかよろしくお願いいたします。

  • ヘッダファイルの外側にメンバ関数を定義する時のinline指定について

    main.cpp、DUT.cppを分割コンパイルした場合、リンク時に main.o(.text+0x0): In function `DUT::FuncA()': <省略>/DUT.h:12: multiple definition of `DUT::FuncA()' DUT.o(.text+0x0):<省略>/DUT.h:12: first defined here collect2: ld returned 1 exit status とエラーが出ます。FuncA関数をインライン指定するとリンクエラーはなくなります。 インライン指定しないと関数の実体がDUT.cpp/main.cppコンパイル毎に生成され、リンク時に衝突してしまう感覚はなんとなくわかるのですが、インライン指定時にFuncA関数の中身はどこに存在しているのでしょうか。 nm/objdumpで見ても見つかりません。 どういうメモリ構造故、インライン指定時には問題が起きていないのか、どこにFuncA関数の実体はあるのかを教えていただけないでしょうか。 // DUT.h #ifndef TLM_DUT_H #define TLM_DUT_H class DUT{ public: void FuncA() ; int ValB ; } ; inline // インライン指定しないとコンパイルエラー void DUT::FuncA(){ ... } #endif // DUT.cpp #include "DUT.h" // main.cpp #include "DUT.h" int main( int, char** ){ return 0; } % make g++ -g -Wall -c DUT.cpp g++ -g -Wall -c main.cpp g++ -g -Wall -o run.x DUT.o main.o 2>&1 | c++filt main.o(.text+0x0): In function `DUT::FuncA()': <省略>/DUT.h:12: multiple definition of `DUT::FuncA()' DUT.o(.text+0x0):<省略>/DUT.h:12: first defined here collect2: ld returned 1 exit status

  • Cppunitのビルドエラー解消

    お世話になります。 Cppunitでテストケースを作成し、ビルドしようとしておりますが、ビルドをすることができません。 ・以下がエラーログの抜粋ですが、「TestRunnerClient.o:TestRunnerClient.cpp:~」の行がEclipse上で赤く表示されており、原因だと思われます。基本的な質問で恐縮ですが、エラーの解消方法をご存知の方がおられればご教示頂けると助かります。 ・プロジェクトのライブラリ指定は添付の画像のようにしております。この部分の指定に誤りはございませんでしょうか。 d000000.o:(.idata$2+0x0): multiple definition of `_head_libcppunit_1_12_1_dll' C:\MinGW\msys\1.0\cppunit-1.12.1\src\cppunit\.libs/libcppunit.dll.a(d000387.o):(.idata$2+0x0): first defined here d000001.o:(.idata$5+0x0): multiple definition of `_imp___ZTVN7CppUnit27TestSuiteBuilderContextBaseE' C:\MinGW\msys\1.0\cppunit-1.12.1\src\cppunit\.libs/libcppunit.dll.a(d001116.o):(.idata$5+0x0): first defined here d000001.o:(.idata$6+0x0): multiple definition of `_nm___ZTVN7CppUnit27TestSuiteBuilderContextBaseE' C:\MinGW\msys\1.0\cppunit-1.12.1\src\cppunit\.libs/libcppunit.dll.a(d001116.o):(.idata$6+0x0): first defined here d000002.o:(.idata$7+0x0): multiple definition of `libcppunit_1_12_1_dll_iname' C:\MinGW\msys\1.0\cppunit-1.12.1\src\cppunit\.libs/libcppunit.dll.a(d001147.o):(.idata$7+0x0): first defined here TestRunnerClient.o:TestRunnerClient.cpp:(.text+0x34f): undefined reference to `send@16' TestRunnerClient.o:TestRunnerClient.cpp:(.text+0x375): undefined reference to `send@16' TestRunnerClient.o:TestRunnerClient.cpp:(.text+0x54f): undefined reference to `send@16' TestRunnerClient.o:TestRunnerClient.cpp:(.text+0x575): undefined reference to `send@16' TestRunnerClient.o:TestRunnerClient.cpp:(.text+0x99d): undefined reference to `gethostname@8' TestRunnerClient.o:TestRunnerClient.cpp:(.text+0x9e8): undefined reference to `gethostname@8' TestRunnerClient.o:TestRunnerClient.cpp:(.text+0xa4b): undefined reference to `WSAStartup@8' TestRunnerClient.o:TestRunnerClient.cpp:(.text+0xa7f): undefined reference to `socket@12' TestRunnerClient.o:TestRunnerClient.cpp:(.text+0xa9b): undefined reference to `gethostbyname@4' TestRunnerClient.o:TestRunnerClient.cpp:(.text+0xae8): undefined reference to `htons@4' TestRunnerClient.o:TestRunnerClient.cpp:(.text+0xb3b): undefined reference to `connect@12' TestRunnerClient.o:TestRunnerClient.cpp:(.text+0xbac): undefined reference to `select@20' TestRunnerClient.o:TestRunnerClient.cpp:(.text+0x121e): undefined reference to `closesocket@4' TestRunnerClient.o:TestRunnerClient.cpp:(.text+0x1226): undefined reference to `WSACleanup@0' TestRunnerClient.o:TestRunnerClient.cpp:(.text+0x138e): undefined reference to `send@16' TestRunnerClient.o:TestRunnerClient.cpp:(.text+0x13b4): undefined reference to `send@16' TestRunnerClient.o:TestRunnerClient.cpp:(.text+0x23ff): undefined reference to `send@16' TestRunnerClient.o:TestRunnerClient.cpp:(.text+0x2425): undefined reference to `send@16' TestRunnerClient.o:TestRunnerClient.cpp:(.text+0x358f): undefined reference to `send@16' TestRunnerClient.o:TestRunnerClient.cpp:(.text+0x35b5): more undefined references to `send@16' follow TestRunnerClient.o:TestRunnerClient.cpp:(.text+0x3a7a): undefined reference to `select@20' c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: TestRunnerClient.o: bad reloc address 0x2 in section `.text$_ZN7CppUnit12TestListenerD1Ev[__ZN7CppUnit12TestListenerD1Ev]'

  • octaveのコンパイルオプション

    MATLABのクローンのoctaveを, C++のライブラリとして利用したいと考えているのですが, コンパイルが上手くいきません. プログラムは以下のような単純なものです. --------- begin of hello.cpp ------------------ #include<iostream> #include<octave/config.h> #include<octave/Matrix.h> int main(){ Matrix m(2,2,1.0); cout<<"hello octave !"<<m; return 0; } --------- end of hello.cpp -------------------- 次のようなコンパイルオプションでコンパイルしています. > g++ -I/usr/include/octave-2.1.35/ -L/usr/lib/octave-2.1.35 \ -loctave -lcruft -loctinterp -lreadline -lcurses -llapack \ -lblas -ldl -lg2c -L/home/pippin/name/.redhat/SOURCES/octave-2.1.35/kpathsea \ -lkpathsea hello.cpp すると,次のようなエラーメッセージを返されます. /tmp/ccuvk2IV.o: In function `main': /tmp/ccuvk2IV.o(/text+0x2c): multiple definition of `main' /usr/lib/gcc-lib/i386-redhat-linux/2.96/libg2c.a(main.o)(.text+0x0): first definition here /usr/bin/ld: Warning: size of symbol `main' changed from 58 to 174 in /tmp/ccuvk2IV.o collect2: ld returned 1 exit status エラーメッセージから解するに,libg2c.aでもmain関数が定義されているため, hello.cppのmain関数とかぶるという様な事だと思うんですが,ライブラリに main関数が定義されているというのも変な話です. ちなみに,コンパイルした環境は, OS: redhat linux 7.3 octave version 2.1.35 gcc version 2.96 このタイプのエラーを回避する方法を知っておられる方は, 回答の方よろしくお願いします.

  • Xcodeでのエラーについて

    MacでOpenCVを利用してKinectを扱おうとしているのですが、以下のようなエラーが出ました。 ちょっと自分には解決できないのですが、どうすればいいか教えてください。 言語はC++です。 Undefined symbols for architecture x86_64: "_cvCreateImage", referenced from: _main in main.o "_cvCvtColor", referenced from: _main in main.o "_cvReleaseImage", referenced from: _main in main.o "_cvShowImage", referenced from: _main in main.o "_cvWaitKey", referenced from: _main in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

  • gdbデバッグのためのmakefile作成

    現在Ubuntu Linuxでgccプログラミングを行っています。現在、ソースコードをいくつかのファイルに分割して作成しています。コンパイル時はmakefileを使用してmakeして実行ファイルを作成しています。 現在のプログラムをgdbでデバッグできるようにしたいと思っているのですが、 その場合のmakefileはどのようにして書けば良いのかをご教示頂きますよう、お願い致します。 現在のmakefile内容 https://sourceforge.jp/projects/cc1101driver/scm/svn/blobs/2319/trunk/20140411/Makefile #test #CC = gcc #test:main.c event.c exe: main.o event.o env.o audio.o task.o comm.o menu_show_status.o gcc -o exe main.o event.o env.o audio.o task.o comm.o menu_show_status.o -lpthread main.o:main.c gcc -c main.c event.o:event.c gcc -c event.c env.o:env.c gcc -c env.c audio.o:audio.c gcc -c audio.c task.o:task.c gcc -c task.c comm.o:comm.c gcc -c comm.c menu_show_status.o:menu_show_status.c gcc -c menu_show_status.c

  • この英語の翻訳を翻訳ソフトを使わずお願いします

    In the chapter we begin by outlining the basic processes you need to go through in order to compile your C programs. We then prceed to formally describe the C compilation model and also how C supports additional libraries. As you know,first we must have a source code that is written in C,and we translate the C instructions into machine instructions.Then preprocessor accepts source code as input and is responsible for removing comments and interpreting special directives. After that,the C compiler translates source code into assembly code. Next,the assembler creates program code(type of file is in object code) from assembly code. Most C program consist of functions that are not intrinsic to the C translator. Functions have to be defined somewhere. Some functions are defined in the program code; some functions are defined in standard libraries-the library code; and some functions are defined in user files-the user code. Both types of file are in object code,and are called LIBRARIES. The object code file corresponding to the original source code will be designated the program code.The object code file(s) that are standard libraries will be designated the library code. The object code file(s) that are converted source code (previously compiled by the user) will be designated the user code. The linker combines these three codes. An executable program is produced by linking together three distinct types of object code program. It is clear that the program is an assembly of disparate elements. よろしくお願いします。

  • エラーについて

    Cプログラミングについての質問です.よろしくお願いします. 以下のヘッダファイル(param.h header.h),および,mainファイル(以下はそのファイルの一部)が正しくコンパイルされるということなのですが,linuxでエラーがでます. エラー内容は error:redifinition of typedef 'gtype_t' error:previous declaration of 'gtype_t' was here です. <param.h> #define F_X pow((x),2)-2 /*探索する関数.関数への入力はx,関数からの出力はy*/ #define G_Y 1/(1+fabs(y)) /*関数の出力に対しての適合度.適合値は高いほどよいものと扱われる,f(x)=0が最良*/ #define GRAY 1 /*グレイ表現なら1,バイナリ表現なら1*/ /*GAのパラメータ群*/ #define MAX (0) /*扱う実数の最大値*/ #define MIN (255) /*扱う実数の最小値*/ #define LENGTH (8) /*遺伝子のコード長*/ #define POP (8) /*個体数*/ #define CODE_MAX 1 /*各遺伝子コードの最大値.これが1ならコードは0か1になる.ビット文字列の場合は1*/ #define GAP 0.7 /*一回の生殖で子供と入れ替わる割合*/ #define ELITE_RATE 0.8 /*そのまま残る数のうち,エリートの割合*/ #define P_MUTATE 0.13 /*突然変異率.LENGTHの逆数程度が良い.*/ #define P_CROSS 1.0 /*交叉確率*/ #define GENERATION 5 /*GAを計算する世代数*/ #define SELECTION_METHOD 1 /*1はルーレット,2はトーナメント*/ #define TOURNAMENT_SIZE 5 /*トーナメントサイズ.トーナメントのときだけ意味がある*/ /*出力*/ #define PRINT_GROUP 1 #define PRINT_FITNESS 1 <header.h> typedef int* gtype_t; typedef struct ga_individual* individual_t; struct ga_individual{ gtype_t gtype; /*遺伝子型 int配列の先頭ポインタ*/ double ptype; /*表現型*/ double fitness; /*適合度*/ individual_t next; /*線形リストでの次の個体*/ int rank; /*線形リストの中での順位(ソート後)*/ int parent1; /*交叉での親1のインデックス*/ int parent2; /*交叉での親2のインデックス*/ int cross_point; /*交叉したポイント*/ }; typedef struct ga_population* ga_population_t; struct ga_population{ individual_t genes; /*個体の線形リスト先頭へのポインタ*/ double *pselect; /*適合度の配列*/ int mutate_count; /*突然変異回数の合計*/ double max_fitness; /*適合度の最大値*/ double min_fitness; /*適合度の最小値*/ double avg_fitness; /*適合度の平均値*/ int population_size; /*集団の個体数*/ int code_length; /*遺伝子長*/ int code_max; /*各遺伝子座の最大値,ビットストリングの場合は1*/ }; <main> /*実数とバイナリ・グレイ表現への変換*/ /*与えられた実数に近い,ビットストリング型のgtypeを作る.*/ /*GRAY=1指定される時はグレイ表現に変換*/ /*最後の桁まで計算した後に残る端数切り捨て*/ typedef int* gtype_t; void encode_gtype(double value,gtype_t gtype,int code_length,double min,double max) { double gap = max-min; double remain_value = value-min; /*値のうち,遺伝子によって表現されている部分*/ double value_of_code; /*その桁の遺伝子が表現する値*/ int position = 1; int pre_code = 0; int i = 0; int tmp; /*グレイ表現変換用,一時保管変数*/ while (i<code_length){ value_of_code = gap/pow(2,position); if(remain_value >= value_of_code){ gtype[i] = 1; remain_value -= value_of_code; } else{ gtype[i] = 0; } /*グレイ表現への変換 バイナリ表現と,元のバイナリを右に1シフトしたもののXORをとる*/ if(GRAY == 1){ tmp = gtype[i]; gtype[i] = (pre_code) ^ (gtype[i]); pre_code = tmp; } position++; i++; } return; } 以上です. よろしくお願い致します.

  • アトメルマイコンでISRを追加すると出てくるエラー

    現在、アトメル社製のマイコンATxmega128A1Uでプログラミングを行なっています。 SPI通信でSPIFポートでの通信を行なっているコードで、受信のコードを割り込みルーチンのようなISRでのコードで受信データを配列に入れるようなコードにしたいと思っています。 そこで、 ISR(SPIF_INT_vect) { } https://github.com/KenjiMaehara/raspberryPiTCPClient/commit/89b4c85451febee0cd841acd531c34cac8112c60#diff-974068d932dfafd8f83f2ef729cab1a3 このような割り込みルーチンのSPIFベクタのコードを追加しましたが、次のようなエラーが発生します。 Error 2 Disabling relaxation: it will not work with multiple definitions 1 1 MainUnit Error 3 ld returned 1 exit status collect2.exe 0 0 MainUnit Error 1 multiple definition of `__vector_118' C:\Users\kmaeh_000\Documents\GitHub\raspberryPiTCPClient\testSpi161101\M1601A_20161205\MainUnit\Debug/.././event.c 87 1 MainUnit このエラーの回避方法をご教示いただきますよう、宜しくお願い致します。

  • クラスについての基本的な質問です

    こんにちは。 クラスを使ってプログラミングしていて、どうしたらいいかわからなくなってしまったので質問させてください。 1つのクラスを2つのクラスが継承している状態で、多重定義エラーが出てしまっています。 ---------- base.h class BaseClass { public: BaseClass(); ~BaseClass(); protected: class InnerClass { public: InnerClass(); virtual ~InnerClass(); } }; ---------- Aclass.h class AClass : public BaseClass { public: AClass(); ~AClass(); }; ---------- BClass.h class BClass : public BaseClass { public: BClass(); ~BClass(); }; ---------- 上記状態で、AClass.cpp、BClass.cpp内で AClass::InnerClass::~InnerClass() { } のようにコンストラクタやデストラクタ等を作っています。 BaseClass.cppにはInnerClassに関するものは書いていません。 「AClassのInnerClassのデストラクタ」というような書き方だと思うのですが、 BClass.o: multiple definition of 'BaseClass::InnerClass::~InnerClass()' AClass.o: previous definition here のように、後にビルドしたものがBaseClassについて多重定義になっているようです。 なぜ多重定義になってしまうのでしょうか? わかりにくいことがあったら言ってください。 補足等しようと思います。 よろしくお願いします。

専門家に質問してみよう