質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

解決済

3回答

5448閲覧

g++のコンパイルでエラー

dampa

総合スコア11

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

0クリップ

投稿2016/11/17 11:59

編集2016/11/18 05:51

###前提・実現したいこと
C++で配列を扱おうと思い、octaveをインストールしました。そしてcppのコードにoctaveに
含まれているものをincludeして簡単な配列を表示しようとしています。
###発生している問題・エラーメッセージ
コンパイルする際に下記のようなエラーが生じてしまいました。

$ g++ main.cpp Undefined symbols for architecture x86_64: "Array<double>::fill(double const&)", referenced from: Array<double>::Array(dim_vector const&, double const&) in main-7e0d24.o "dim_vector::safe_numel() const", referenced from: Array<double>::Array(dim_vector const&) in main-7e0d24.o Array<double>::Array(dim_vector const&, double const&) in main-7e0d24.o "ColumnVector::transpose() const", referenced from: _main in main-7e0d24.o "Array<double>::resize_fill_value() const", referenced from: vtable for Matrix in main-7e0d24.o vtable for NDArray in main-7e0d24.o vtable for MArray<double> in main-7e0d24.o vtable for Array<double> in main-7e0d24.o vtable for ColumnVector in main-7e0d24.o "operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, Matrix const&)", referenced from: _main in main-7e0d24.o "operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, RowVector const&)", referenced from: _main in main-7e0d24.o "operator*(Matrix const&, ColumnVector const&)", referenced from: _main in main-7e0d24.o "MArray<double> operator+<double>(MArray<double> const&, MArray<double> const&)", referenced from: operator+(ColumnVector const&, ColumnVector const&) in main-7e0d24.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

###該当のソースコード

cpp

1#include <octave/config.h> 2#include <octave/Matrix.h> 3#include <iostream> 4using namespace std; 5int main(int argc, char**argv) 6{ 7 Matrix A(2,3), B(2,2,1.0); 8 ColumnVector x(3), y(2), z(2); 9 for(int r(0); r<2; ++r) 10 for(int c(0); c<3; ++c) 11 A(r,c) = r+c; 12 for(int r(0); r<3; ++r) x(r)=(r+1.0)*2.0; 13 for(int r(0); r<2; ++r) y(r)=(r+1.0)*r; 14 z = B*(A*x+y); 15 cout<<"A="<<endl; 16 cout<<A; 17 cout<<"B="<<endl; 18 cout<<B; 19 cout<<"x= "<<x.transpose()<<endl; 20 cout<<"y="<<y.transpose()<<endl; 21 cout<<"z="<<z.transpose()<<endl; 22 return 0; 23}

###試したこと
includeとlibはしっかりと読み込めるように、コンパイルされている場所にエイリアスを作成しました。ですが、この問題の解決にはつながらなかったようです。
Opencv以外には他のライブラリを利用したことがないようなcppの初心者ですので、わかりやすく教えていただけると幸いです。コードはこちらのサイトに記載されていたものを参考にさせてもらいました。http://d.hatena.ne.jp/aki-yam/20080721/1218193668
よろしくお願いします。
###補足情報(言語/FW/ツール等のバージョンなど)
octave 4.0.3

###追記
回答していただいた内容を活かして修正したコンパイルのログです。
ヘッダとライブラリを含めてコンパイルはできたと思うのですが、どうにもエラーがなくなりません。

gcc -g -Wall main.cpp -I/usr/local/include/octave-4.2.0/octave -I/usr/local/Cellar/octave/4.2.0/binoctave-'octave-config -v' -L/usr/local/lib/octave/4.2.0 -O0 In file included from main.cpp:2: /usr/local/include/octave-4.2.0/octave/config.h:28:2: warning: "config.h has been deprecated; there is no need to include it directly" [-W#warnings] #warning "config.h has been deprecated; there is no need to include it directly" ^ In file included from main.cpp:3: In file included from /usr/local/include/octave-4.2.0/octave/Matrix.h:32: In file included from /usr/local/include/octave-4.2.0/octave/mx-base.h:30: In file included from /usr/local/include/octave-4.2.0/octave/MatrixType.h:29: In file included from /usr/local/include/octave-4.2.0/octave/MSparse.h:31: In file included from /usr/local/include/octave-4.2.0/octave/Sparse.h:37: In file included from /usr/local/include/octave-4.2.0/octave/Array.h:37: /usr/local/include/octave-4.2.0/octave/dim-vector.h:205:21: warning: variadic templates are a C++11 extension [-Wc++11-extensions] template <typename... Ints> ^ /usr/local/include/octave-4.2.0/octave/dim-vector.h:209:10: error: no member named 'initializer_list' in namespace 'std' std::initializer_list<octave_idx_type> all_lengths = {r, c, lengths...}; ~~~~~^ /usr/local/include/octave-4.2.0/octave/dim-vector.h:209:27: error: unexpected type name 'octave_idx_type': expected expression std::initializer_list<octave_idx_type> all_lengths = {r, c, lengths...}; ^ /usr/local/include/octave-4.2.0/octave/dim-vector.h:209:44: error: use of undeclared identifier 'all_lengths' std::initializer_list<octave_idx_type> all_lengths = {r, c, lengths...}; ^ /usr/local/include/octave-4.2.0/octave/dim-vector.h:209:58: error: expected expression std::initializer_list<octave_idx_type> all_lengths = {r, c, lengths...}; ^ /usr/local/include/octave-4.2.0/octave/dim-vector.h:210:33: warning: range-based for loop is a C++11 extension [-Wc++11-extensions] for (const octave_idx_type l: all_lengths) ^ /usr/local/include/octave-4.2.0/octave/dim-vector.h:210:35: error: use of undeclared identifier 'all_lengths' for (const octave_idx_type l: all_lengths) ^ /usr/local/include/octave-4.2.0/octave/dim-vector.h:212:12: error: use of undeclared identifier 'all_lengths' rep -= all_lengths.size (); ^ In file included from main.cpp:3: In file included from /usr/local/include/octave-4.2.0/octave/Matrix.h:32: In file included from /usr/local/include/octave-4.2.0/octave/mx-base.h:30: In file included from /usr/local/include/octave-4.2.0/octave/MatrixType.h:29: In file included from /usr/local/include/octave-4.2.0/octave/MSparse.h:31: In file included from /usr/local/include/octave-4.2.0/octave/Sparse.h:37: /usr/local/include/octave-4.2.0/octave/Array.h:282:30: warning: variadic templates are a C++11 extension [-Wc++11-extensions] template<template <typename...> class Container> ^ /usr/local/include/octave-4.2.0/octave/Array.h:861:28: warning: variadic templates are a C++11 extension [-Wc++11-extensions] template<template <typename...> class Container> ^ /usr/local/include/octave-4.2.0/octave/Array.h:876:19: warning: range-based for loop is a C++11 extension [-Wc++11-extensions] for (const T& x : a) ^ In file included from main.cpp:3: In file included from /usr/local/include/octave-4.2.0/octave/Matrix.h:32: In file included from /usr/local/include/octave-4.2.0/octave/mx-base.h:35: In file included from /usr/local/include/octave-4.2.0/octave/chMatrix.h:32: In file included from /usr/local/include/octave-4.2.0/octave/chNDArray.h:35: /usr/local/include/octave-4.2.0/octave/str-vec.h:59:30: warning: variadic templates are a C++11 extension [-Wc++11-extensions] template<template <typename...> class String_Container, typename... Other> ^ /usr/local/include/octave-4.2.0/octave/str-vec.h:59:67: warning: variadic templates are a C++11 extension [-Wc++11-extensions] template<template <typename...> class String_Container, typename... Other> ^ /usr/local/include/octave-4.2.0/octave/str-vec.h:130:28: warning: variadic templates are a C++11 extension [-Wc++11-extensions] template<template <typename...> class String_Container, typename... Other> ^ /usr/local/include/octave-4.2.0/octave/str-vec.h:130:65: warning: variadic templates are a C++11 extension [-Wc++11-extensions] template<template <typename...> class String_Container, typename... Other> ^ /usr/local/include/octave-4.2.0/octave/str-vec.h:138:29: warning: range-based for loop is a C++11 extension [-Wc++11-extensions] for (const std::string& s : lst) ^ In file included from main.cpp:3: In file included from /usr/local/include/octave-4.2.0/octave/Matrix.h:34: In file included from /usr/local/include/octave-4.2.0/octave/mx-ext.h:54: /usr/local/include/octave-4.2.0/octave/svd.h:43:12: warning: scoped enumerations are a C++11 extension [-Wc++11-extensions] enum class Type ^ /usr/local/include/octave-4.2.0/octave/svd.h:50:12: warning: scoped enumerations are a C++11 extension [-Wc++11-extensions] enum class Driver ^ 13 warnings and 6 errors generated.

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答3

0

こんにちは。

includeとlibはしっかりと読み込めるように、コンパイルされている場所にエイリアスを作成しました。

ヘッダ・ファイルは標準ライブラリと同じ位置に配置すれば、ユーザ・プログラムの#includeでインクルードされますが、ライブラリは明示的に指定しないとリンクされません。

g++ -g -Wall -I/usr/include/octave-`octave-config -v` -L/usr/lib/octave-`octave-config -v` -loctave -lcruft hello-octave.cpp

また、上記のようなコマンドが必要ということは、ヘッダ・ファイルを安易に標準ライブラリと同じ位置にコピーしても、うまくいかないと思われます。octave-config -vは、たぶん、PCのアーキテクチャを調べて適切なヘッダとライブラリを選択するためのおまじないと思います。

投稿2016/11/17 12:55

Chironian

総合スコア23272

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

dampa

2016/11/17 13:09

丁寧に説明してくださりありがとうございます。 ご指摘いただいたようにコンパイルしてみたのですが、またエラーとなりました。 ``` $ g++ -g -Wall -I/usr/local/include/octave-`octave-config -v` -L/usr/local/lib/octave-`octave-config -v` -loctave -lcruft main.cpp -bash: octave-config: command not found -bash: octave-config: command not found ld: warning: directory not found for option '-L/usr/local/lib/octave-' ld: library not found for -loctave clang: error: linker command failed with exit code 1 (use -v to see invocation) ``` このようなエラーは、おまじないのアクセス先が間違えているということでしょうか?
dampa

2016/11/18 05:46 編集

パスを確認し直して、ヘッダとライブラリを含めてコンパイルはできたように思います。 ですが、またエラーが出てしまいました... octaveを再インストールする際に新しいバージョンにしたので、"config.h"が必要ないと言われていますが、そこは本質ではないと考えています。 コンパイルした結果は質問欄に追記しました。 ヘッダの中身の部分にエラーがあると言われてしまって、何に手をつけていいのかわからないです。 これもまた、何かコンパイルの間違いなのでしょうか? 重ねて質問して申し訳ありません。
Chironian

2016/11/18 07:17

見ているサイトが2008年の情報ですので、最新のoctaveに対応していないと言うことですね。 octaveを当時の古いバージョンにするか、最新に近いoctaveのサンプルが載っているサイトを探すのが妥当と思います。 ただ、エラー・メッセージに従えば、下記である程度減るはずです。 ①#include <octave/config.h>をコメントアウトする ②コンパル時にオプション-std=c++11を追加する。 後者は、C++11規格に従ってコンパルするようにgccへ指示するものです。 (https://ja.wikipedia.org/wiki/C%2B%2B11)
dampa

2016/11/18 15:33

コンパイルオプション-std=c++11が大変助かりました。検索の仕方が悪かったのか、そのような対策を見つけられず困っていました。 そのオプションを活かして修正しようとしていたのですが、やはり古いバージョンであることが原因でうまくいってないようでした。 最新のバージョンに対応しているマニュアルを読むことで解決しました。 解決方法は他の人が見てもわかりやすいように丁寧に書きたいので、もう少し時間が経ってから載せます。 最終的にはoctaveから提供されてコンパイルコマンドで解決することができました。 ですが、今回教えていただいたことで大変勉強になりました。 丁寧に回答してくださり、ありがとうございました!
guest

0

パッと見ライブラリが足りてないです。
コマンドラインに-lなんとか(おそらく -loctave)が必要なんだと思われ。

...てか、その参考pageに書いてあるじゃん!

投稿2016/11/17 12:15

編集2016/11/17 12:18
episteme

総合スコア16614

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

dampa

2016/11/17 13:09

回答くださりありがとうございます。 ご指摘いただいたように修正してみたつもりなのですが、なかなかうまくいきません... Chironianさんの回答欄にまたコメントを書き足しますので、そちらを読んでまた返信いただけると幸いです。
guest

0

自己解決

圧縮ファイルをダウンロードして展開した際に、パスがしっかりと通っていない可能性があったので一度全て削除しました。

それから
$brew install octave
で最新版のoctaveをダウロード&インストールしました。

参考にしていたサイトの情報は古く、そこに記載されていたサンプルコードではコンパイルできないようでしたので、最新のオンラインマニュアルをしっかりと目を通しました。
その中にサンプルコードがありましたので、それでコードを作りました。サンプルコードのページはこちらです。
https://www.gnu.org/software/octave/doc/v4.0.1/Standalone-Programs.html#Standalone-Programs

また、通常はコンパイルの際に-Lや-Iによるオプションでリンク先を明示する必要があります。ですが、マニュアルによると
「C ++コンパイラを見つける、正しいスイッチを使う、ヘッダファイルのための正しいインクルードパスを加えることなどは難しい作業です。 Octaveは、mkoctfileコマンドを提供してこれを自動化して、oct-fileをビルドします。」
ということですので、mkoctfileでコンパイルするだけで解決できました。
このことが説明されているページはこちらです。
https://www.gnu.org/software/octave/doc/v4.0.1/Getting-Started-with-Oct_002dFiles.html#Getting-Started-with-Oct_002dFiles

下に実際に作成したコードとコンパイル結果を載せておきます。

c++

1#include <iostream> 2#include <octave/oct.h> 3 4int 5main (void) 6{ 7 std::cout << "Hello Octave world!\n"; 8 9 int n = 2; 10 Matrix a_matrix = Matrix (n, n); 11 12 for (octave_idx_type i = 0; i < n; i++) 13 for (octave_idx_type j = 0; j < n; j++) 14 a_matrix(i,j) = (i + 1) * 10 + (j + 1); 15 16 std::cout << a_matrix; 17 18 return 0; 19}
$ mkoctfile --link-stand-alone standalone.cc clang: warning: argument unused during compilation: '-pthread' $ ./standalone Hello Octave world! 11 12 21 22

解決にご協力してくださった皆様、ありがとうございました。

投稿2016/11/20 04:24

dampa

総合スコア11

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問