アプリケーション開発ポータルサイト
ServerNote.NET
Amazon.co.jpでPC関連商品タイムセール開催中!
カテゴリー【C/C++PythonUbuntuDebian
nvccでerror: attribute __malloc__ does not take argumentsとなる場合の対処法
POSTED BY
2023-11-26

表題のエラーは、Ubuntu等Linux環境でNVIDIA nvccを走らせると起こる。

これは、nvccが呼んでいるgcc/g++のバージョンが高すぎるために起こっているので、複数バージョンのgcc/g++をインストールして使い分けることで解決する。

現バージョンのnvccはgcc10以下でないと表題のエラーになることから、10を入れてalternativisでリンクを10に向けて管理すれば、あとで簡単に11に戻せる。

インストール可能なバージョンの確認

sudo -s

apt search '^gcc-[0-9]+$'

Sorting... Done
Full Text Search... Done
gcc-11/impish,now 11.2.0-7ubuntu2 amd64 [installed]
  GNU C compiler

gcc-10/impish,now 10.3.0-11ubuntu1 amd64
  GNU C compiler

gcc-8/impish 8.5.0-0ubuntu4 amd64
  GNU C compiler

gcc-9/impish 9.4.0-3ubuntu1 amd64
  GNU C compiler

11がインストールされているので、エラーを解決するためまず普通に10を入れる。

apt install gcc-10
apt install g++-10

update-alternativesで、10と11をリンク登録し、切り替えられるようにする。

update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 10
update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-10 10
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 11
update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 11

登録したらconfigで、バージョンを選ぶ。

update-alternatives --config gcc
There are 2 choices for the alternative gcc (providing /usr/bin/gcc).

  Selection    Path             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/gcc-11   11        auto mode
  1            /usr/bin/gcc-10   10        manual mode
  2            /usr/bin/gcc-11   11        manual mode

Press <enter> to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/gcc-10 to provide /usr/bin/gcc (gcc) in manual mode

update-alternatives --config gcc
There are 2 choices for the alternative gcc (providing /usr/bin/gcc).

  Selection    Path             Priority   Status
------------------------------------------------------------
  0            /usr/bin/gcc-11   11        auto mode
* 1            /usr/bin/gcc-10   10        manual mode
  2            /usr/bin/gcc-11   11        manual mode

update-alternatives --config g++
There are 2 choices for the alternative g++ (providing /usr/bin/g++).

  Selection    Path             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/g++-11   11        auto mode
  1            /usr/bin/g++-10   10        manual mode
  2            /usr/bin/g++-11   11        manual mode

Press <enter> to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/g++-10 to provide /usr/bin/g++ (g++) in manual mode

update-alternatives --config g++
There are 2 choices for the alternative g++ (providing /usr/bin/g++).

  Selection    Path             Priority   Status
------------------------------------------------------------
  0            /usr/bin/g++-11   11        auto mode
* 1            /usr/bin/g++-10   10        manual mode
  2            /usr/bin/g++-11   11        manual mode

gcc, g++それぞれで行って、デフォルトでは最新11にリンクが向けられているので、1をタイプして、10を選べば、コンパイラバージョンは10で動く。簡単に11に戻せるし、超便利!さすLinux!

gcc --version
gcc (Ubuntu 10.3.0-11ubuntu1) 10.3.0
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

g++ --version
g++ (Ubuntu 10.3.0-11ubuntu1) 10.3.0
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
※本記事は当サイト管理人の個人的な備忘録です。本記事の参照又は付随ソースコード利用後にいかなる損害が発生しても当サイト及び管理人は一切責任を負いません。
※本記事内容の無断転載を禁じます。
【WEBMASTER/管理人】
自営業プログラマーです。お仕事ください!
ご連絡は以下アドレスまでお願いします★

☆ServerNote.NETショッピング↓
ShoppingNote / Amazon.co.jp
☆お仲間ブログ↓
一人社長の不動産業務日誌
【キーワード検索】