0

重複の可能性:
未定義の参照/未解決の外部シンボルエラーとは何ですか?それを修正するにはどうすればよいですか?

私が持っているmain.cpp

#include "censorship_dec.h"

using namespace std;

int main () {
    censorship();
    return 0;
}

これは私のcensorship_dec.hです:

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

void censorship();

これは私のcensorship_mng.cppです:

#include "censorship_dec.h"
using namespace std;

void censorship()
{
   cout << "bla bla bla" << endl;
}

これらのファイルをSSH(Linux)で実行しようとしたので、次のように記述しmake mainました。

g++     main.cpp   -o main
/tmp/ccULJJMO.o: In function `main':
main.cpp:(.text+0x71): undefined reference to `censorship()'
collect2: ld returned 1 exit status
make: *** [main] Error 1

助けてください!

4

3 に答える 3

5

censorshipが定義されているファイルを指定する必要があります。

g++ main.cpp censorship_mng.cpp -o main
于 2013-01-15T11:37:51.117 に答える
3

censorship_mng.cppコンパイルコマンドを追加する必要があります。

g ++ main.cpp censorship_mng.cpp -o main


別の解決策(コンパイルコマンドを本当に変更したくない場合)はvoid censorship();inline関数を作成してから.cppに移動すること.hです。

censorship_dec.h

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

inline void censorship()
{
  // your code
}

そして、ファイルから削除void censorship()censorship_mng.cppます。

于 2013-01-15T11:37:54.547 に答える
0

プロジェクトが複数のソースファイルを使用して単一のバイナリにコンパイルし始めると、手動でのコンパイルは面倒になります。

これは通常、 Makefileなどのビルドシステムの使用を開始する時間です。

デフォルトのビルドルールを使用する非常に単純なMakefileは、次のようになります。

default: main

# these flags are here only for illustration purposes
CPPFLAGS=-I/usr/include
CFLAGS=-g -O3
CXXFLAGS=-g -O3
LDFLAGS=-lm

# objects (.o files) will be compiled automatically from matching .c and .cpp files
OBJECTS=bar.o bla.o foo.o main.o

# application "main" build-depends on all the objects (and linksthem together)
main: $(OBJECTS)
于 2013-01-15T11:49:49.460 に答える