Linuxでリンクを貼る方法とコンパイルエラーの解決

このQ&Aのポイント
  • Linuxでリンクを貼る方法やコンパイルエラーの解決方法について解説します。
  • プログラムのコンパイルエラーが発生した場合、関数の宣言や定義の問題が考えられます。
  • Linux環境でプログラムをコンパイルする際には、関数の宣言や定義を正しく行う必要があります。
回答を見る
  • ベストアンサー

linuxでリンクを貼るには

linuxでリンクを貼るには 本に書いてある通りにプログラムをつくったのですが、 うまくコンパイルできません。 本で書いてあるのはwindowsのvisual C++で、 僕の使っている環境はlinuxのg++です。 コンパイルした結果を以下に示します。 -- ファイル名:myfunc.h //declaration of the max function int max(int x, int y); -- ファイル名:myfunc.cpp //a definition of the max function int max(int x, int y) { if(x > y) return x; else return y; } -- ファイル名:sample10.5.cpp #include <iostream> #include "myfunc.h" using namespace std; int main() { int num1, num2, ans; cout << "Please input the 1st integer.\n"; cin >> num1; cout << "Please input the 2nd integer.\n"; cin >> num2; ans = max(num1, num2); cout << "The maximum value is " << ans << '\n'; return 0; } -- bash-2.05b$ g++ myfunc.cpp /usr/lib/gcc-lib/i386-redhat-linux/3.2.3/../../../crt1.o(.text+0x18): In function `_start': : undefined reference to `main' collect2: ld はステータス 1 で終了しました -- bash-2.05b$ g++ sample10.5.cpp /tmp/ccWJNOkX.o(.text+0x6c): In function `main': : undefined reference to `max(int, int)' collect2: ld はステータス 1 で終了しました

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

  • ベストアンサー
  • kmee
  • ベストアンサー率55% (1857/3366)
回答No.3

Visual Cとかだと、IDE側でまとめてやってくれるので、そういう説明が無いこともあるでしょうね。 方法は2つ。 1) g++ sample10.5.cpp myfunc.cpp と続けて書く 2) g++ -c sample10.5.cpp g++ -c myfunc.cpp と -c オプションを付けてオブジェクトファイルにコンパイルする(拡張子 .o ) → g++ sample10.5.o myfunc.o でリンク 数が少ないときは1)でよいですが、大規模になってくると、更新した分だけコンパイルすればよい 2)の方法がいいです。 依存関係をMakefileに書いておけば、 make コマンド一つで更新されたものだけコンパイルしてくれます。

ishigamin
質問者

お礼

ご回答頂きありがとうございます。 2つの方法があるのですね。 とても参考になりました。

その他の回答 (2)

  • mtaka2
  • ベストアンサー率73% (867/1179)
回答No.2

g++ sample10.5.cpp myfunc.cpp とするか、コンパイルとリンクを分けて、 g++ -c sample10.5.cpp g++ -c myfunc.cpp g++ sample10.5.o myfunc.o の3コマンドを実行することも可能。 こうすると変更していないファイルのコンパイルが要らないので時間短縮になります。 さらに、make というコマンドを使えば、このあたりの処理を自動化できます。 (VisualStudio は、単なるコンパイラではなく、make 相当の機能も持っています)

ishigamin
質問者

お礼

ご回答頂きありがとうございます。 無事処理ができました。 makeということも勉強してみます。

  • D-Matsu
  • ベストアンサー率45% (1080/2394)
回答No.1

そこで、両方一緒に引数で渡してみる、というのを試す気にはなれませんでしたか?

ishigamin
質問者

お礼

ご回答頂きありがとうございます。 初心者なもので、引数を2つ取れるというアイディアは浮かびませんでした。

関連するQ&A

  • {x = x>y ? x:y; return x;}

    #include <iostream> using namespace std; inline int max(int x, int y){x = x>y ? x:y; return x;} int main() { int num1, num2, ans; cout << "2つの整数を入力して。\n"; cin >> num1 >> num2; ans = max(num1, num2); cout << "最大値は" << ans << "です。\n"; return 0; } の{x = x>y ? x:y; return x;}の部分の意味が解りません。

  • インライン関数の使い道と理屈

    #include <iostream> using namespace std; //max関数の定義 inline int max(int x, int y){if(x>y) return x; else return y;} int main(){ int num1, num2, ans; cout << "1番目の整数を入力して下さい。\n"; cin >> num1; cout <<"2番目の整数を入力して下さい。\n"; cin >> num2; ans = max(num1, num2); cout << "最大値は" << ans << "です。\n"; return 0; } インライン関数の処理は呼び出し部分に埋め込まれるので、プログラムの処理速度が 向上することがありますとあるんですが、 どのような時にインライン関数は使用するのでしょうか? 理由もしくみもご教示お願いします。

  • 合計値を求める関数

    #include<iostream> using namespace std; //sum関数の定義 int sum(int x, int y) { return x + y;  } int main() { int num1, num2, ans; cout << "1番目の整数を入力して下さい。\n"; cin >> num1; cout << "2番目の整数値を入力して下さい。\n"; cin >> num2; ans = sum(num1, num2); cout << "合計は" << ans << "です。\n"; return 0; }  ここのreturn x+y;の所の合計値を戻り値として返す処理の仕組みを解りやすく教えて欲しいです、戻り値はちょっと解りづらいです、よろしくお願いします。

  • C言語/プログラミング

    int Trianglearea(int x, y) { return x * y / 2; } int main() { int x, y, ans; cout << "底辺を入力してください。\n"; cin >> x; cout << "高さを入力してください。\n"; cin >> y; cout << "三角形の面積は" <<Trianglearea(x, y) << "です。\n"; return 0; } これで、実行したらエラー発生しました。エラーを治す方法を教えてください。

  • 分割コンパイルの手順と方法について

    Visual Studio 2010でファイルを分割してコンパイル、リンクする方法がわかりません。以下のような3つのファイルです。 [myfunc.h] /* max関数の宣言 */ int max(int x, int y); [myfunc.c] int max(int x, int y) { if (x > y) return x; else return y; } [Sample13.c] #include <stdio.h> #include "myfunc.h" int main(void) { int num1, num2, ans; printf("1番目の整数を入力してください。\n"); scanf("%d", &num1); printf("2番目の整数を入力してください。\n"); scanf("%d", &num2); ans = max(num1, num2); printf("最大値は%dです。\n", ans); return 0; } 以上3つのファイルはあくまでも便宜的なもので、複数のファイルのコンパイル・リンクの手順を具台的に理解するために 用意したものです。

  • C++で解りません。

    C++で #include <iostream.h> int main(void) { int x; int y; cout << "xを入力してください:"; cin >> x; cout << "yを入力してください:"; cin >> y; cout << "x+yは" << x + y << "です。\n"; return 0; } ----------- で、 エラー E2206 a.cpp 19: 不正な文字 ' ' (0×8140) エラー E2206 a.cpp 19: 不正な文字 ' ' (0×8140) エラー E2206 a.cpp 19: 不正な文字 ' ' (0×8140) エラー E2206 a.cpp 19: 不正な文字 ' ' (0×8140) エラー E2206 a.cpp 19: 不正な文字 ' ' (0×8140) エラー E2206 a.cpp 19: 不正な文字 ' ' (0×8140) とでます。どこがおかしいのか解りません。

  • 実行

    こんにちは。私は昨日からC++を始めたのですがなかなかうまくいかないためこちらに質問させていただくことにしました。 #include <iostream.h> int main(void) { int x; int y; cout<<"xを入力: 10"; cin>>x; cout<<"yを入力: 2"; cin>>y; cout<<"x+yは"<<x+y<<"です。\n"; return(0); } このようなプログラムを作成したのですが実行してみたところ、 xを入力: 10 これ以降下までいきません。どうすればうまくいくのでしょうか?宜しくお願いします。

  • 設定した値が意図せぬ値に

    POJ 3176の問題です。 http://poj.org/problem?id=3176 #include <iostream> #include <algorithm> #define MAX 100 using namespace std; int main() { int n; cin >> n; int line[n-1][MAX]; int num[n-1][MAX]; cin >> line[0][0]; if(n==0) { cout << 0 <<endl; return 0; } else if(n==1) { cout << line[0][0] << endl; return 0; } for (int i =1;i < n;i++) { for (int j=0;j < i+1;j++) { int x; cin >> x; line[i][j]= x; } } for (int k= 0 ; k<n;k++) { num[n-1][k]= line [n-1][k]; } for (int k= n-2; k > 0 ; k--) { for (int l=0 ; l<k+1; l++) { num[k][l] = max (num[k+1][l],num[k+1][l+1]) + line[k][l]; } } num[0][0] = max(num[1][0],num[1][1]) + line[0][0]; cout << line[0][0] <<" "<<num[0][0]<<endl; return 0; } 入力 4 3 1 3 1 2 3 1 3 4 5 出力 1 12 最後に出力でline[0][0]をするようにしているのはバグチェックのためです。 ここで僕がわからないのはどうしてline[0][0]が3で宣言し、ほかでいじっていないにも関わらず、最後に1になっているのかということです。 どなたかわかる方がいらっしゃったらよろしくお願いします。

  • プログラムのファイル分割。

    現在C++を学んでいるものです。 ソースファイルの分割についての質問です。よろしくお願いします。 環境はVisual Studio 2005です。 ファイルを      myheader.h      main.cpp      function.cpp と分ける事を考えると、例えば単純な int max(int num1, int num2){ if(num1 > num2) return num1; else return num2; } という関数を考えた場合、 myheader.hには関数maxの宣言int max(int num1, int num2);を。 function.cppには上記の関数maxの内容を。 そしてmain.cppで関数maxを使用するといったように分割し、実行することまでは自分でできます。 ですが、上記の関数をテンプレートを使って、どの型でも使用可能にするには、myheader.h、function.cppにはどのように記述したらよいでしょうか? テンプレートを使った関数は普通ですが、 template <class T> T max(T num1, Tnum2){ if(num1 > num2) return num1; else return num2; } というように記述したいと考えています。 よろしくお願いします

  • ファイル分割について

    今本をみながら練習中なのですが、ファイル分割がうまくいきません。以下のような3つのプログラムをかいたのですが実行できません。 (一つ目:myfunc.h) int max(int x,int y); (二つ目:myfunc.c) int max(int x,int y){ if(x > y) return x; else return y;} (三つ目:sample.c) #include <stdio.h> #include "myfunc.h" int main(void){ int x,y,c; printf("1番目の整数\n"); scanf("%d",&x); printf("2番目の整数\n"); scanf("%d",&y); c = max(x,y); printf("最大値は%d\n",c); return 0;} すべてコンパイルしてsample.cを実行してもだめでした。ご指導お願いします><