152

When I compile C/C++ program with popen in php... I got this error:

g++: error trying to exec 'cc1plus': execvp: No such file or directory

but if I run php code in shell.. it works fine..

in Arch Linux..

PHP Code:

<?php
    function rfile($fp) {
    $out="";
       while (!feof($fp)) {
           $out.= fgets($fp, 1024000);
       }
       return $out;
    }
    $p = popen('g++ -Wall -g aplusb.cc -o aplusb 2>&1', 'r');
    $result = rfile($p);
    pclose($p);
    echo $result;
?>

thanks

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
Zeyi Fan
  • 2,213
  • 3
  • 17
  • 19
  • 2
    Have you tried to print env variables and compare them? Do you have safe mode on or off? – Vyktor Jan 16 '12 at 10:34
  • yes.. I compared the env variables between php and shell ... but it dont have any help... and my safe mode is Off.. – Zeyi Fan Jan 16 '12 at 10:38
  • Are you using the same user or executing script from web server? Add "-v" (should be verbose output), maybe there'll be an answer. – Vyktor Jan 16 '12 at 10:43
  • ok.. i just tested to compile C++ code and run `php xx.php` as `http` user. all of them is success... and the output of `g++ -v` in php code is similar with its in shell.. – Zeyi Fan Jan 16 '12 at 11:02
  • 1
    `gcc -print-search-dirs` how about this? Are the outputs **the same**? – Vyktor Jan 16 '12 at 11:10
  • Try to add `-v` to your `gcc` command inside PHP and look at the result (it will show what is happenning). – Basile Starynkevitch Jan 16 '12 at 11:14
  • the difference of `gcc -v` is `> COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-pc-linux-gnu/4.6.2/lto-wrapper`... and the outputs of `gcc -print-search-dirs` is same... – Zeyi Fan Jan 16 '12 at 11:19
  • It is exasperating that the subject refers to a C compilation error, but the body of the question refers to a C++ compilation error. – Jonathan Leffler Dec 09 '16 at 18:14
  • @JonathanLeffler Sorry for the confusion, but I remember when I asked this question, both C and C++ programs can not be compiled. – Zeyi Fan Dec 12 '16 at 00:07

11 Answers11

197

You need to install gcc-c++ package.

yum install gcc-c++
hahakubile
  • 6,978
  • 4
  • 28
  • 18
96

I don't know why but i just renamed my source file COLARR.C to colarr.c and the error vanished! probably you need this

sudo apt-get install g++
Sunil Kumar
  • 6,112
  • 6
  • 36
  • 40
  • 3
    Note that upper-case `.C` extension is one of a number of conventions for C++ source — `.cpp` and `.cc` are two others. The upper-case `.C` convention interacts badly with case-insensitive file systems (Windows, macOS, for example). When you had `COLARR.C`, the system was probably looking at it as a C++ source file; as `colarr.c`, it is a C source file. – Jonathan Leffler Dec 09 '16 at 18:13
35

This problem can happen if different versions of g++ and gcc are installed.

   g++ --version
   gcc --version

If these don't give the result, you probably have multiple versions of gcc installed. You can check by using:

    dpkg -l | grep gcc | awk '{print $2}'

Usually, /usr/bin/gcc will be sym-linked to /etc/alternatives/gcc which is again sym-linked to say /usr/bin/gcc-4.6 or /usr/bin/gcc-4.8 (In case you have gcc-4.6, gcc-4.8 installed.)

By changing this link you can make gcc and g++ run in the same version and this may resolve your issue!

pulkitag
  • 605
  • 6
  • 10
  • This does not solve the problem, I did not find a way to change these links. – Brana Jul 23 '19 at 01:44
  • It worked, and the error goes away after I modify the softlink of /etc/alternatives/gcc from /usr/bin/gcc72 to /usr/bin/gcc48 with ln -fs /usr/bin/gcc48 /etc/alternatives/gcc. – buxizhizhoum Nov 29 '19 at 01:33
  • 1
    This was my case. I installed a new version of gcc and symlinked the gcc binary but forgot to symlink the g++ binary as well. Just ensure `gcc -v` and `g++ -v` show the same version. – Rahul Bharadwaj Nov 23 '21 at 06:47
  • Just happened with me, and I needed to make the same version for both gcc and g++ , gcc was in 12 while g++ was only in 11 – Abdelouahab Jan 29 '23 at 13:16
10

Each compiler has its own libexec/ directory. Normally libexec directory contains small helper programs called by other programs. In this case, gcc is looking for its own 'cc1' compiler. Your machine may contains different versions of gcc, and each version should have its own 'cc1'. Normally these compilers are located on:


/usr/local/libexec/gcc/<architecture>/<compiler>/<compiler_version>/cc1

Similar path for g++. Above error means, that the current gcc version used is not able to find its own 'cc1' compiler. This normally points to a PATH issue.

Freddy
  • 3,064
  • 3
  • 26
  • 27
  • I had the same issue with my PATH environment when I ran `strace g++ [args]` I discovered it was trying the wrong folder in the path then giving up. – sirbrialliance Jun 16 '12 at 03:27
4

For apk, easiest way is:

apk add build-base

Myotha
  • 66
  • 4
3

Install g++ on openSuSE run

zypper in gcc-c++
David Hamner
  • 671
  • 5
  • 6
2

I had the same issue when forking with 'python'; the main reason is that the search path is relative, if you don't call g++ as /usr/bin/g++, it will not be able to work out the canonical paths to call cc1plus.

vliu
  • 21
  • 1
1

Something went wrong with your GCC installation. Try reinstalling the it like this:

sudo apt-get install --reinstall g++-5

In Ubuntu the g++ is a dependency package that installs the default version of g++ for your OS version. So simply removing and installing the package again won't work, cause it will install the default version. That's why you need to reinstall.

Note: You can replace the g++-5 with your desired g++ version. To find your current g++ version run this:

g++ --version
tsveti_iko
  • 6,834
  • 3
  • 47
  • 39
1

You may have this issue as well if you have environment variable GCC_ROOT pointing to a wrong location. Probably simplest fix could be (on *nix like system):

unset GCC_ROOT

in more complicated cases you may need to repoint it to proper location

Slava
  • 43,454
  • 1
  • 47
  • 90
0

I had the same issue with gcc "gnat1" and it was due to the path being wrong. Gnat1 was on version 4.6 but I was executing version 4.8.1, which I had installed. As a temporary solution, I copied gnat1 from 4.6 and pasted under the 4.8.1 folder.

The path to gcc on my computer is /usr/lib/gcc/i686-linux-gnu/

You can find the path by using the find command:

find /usr -name "gnat1"

In your case you would look for cc1plus:

find /usr -name "cc1plus"

Of course, this is a quick solution and a more solid answer would be fixing the broken path.

ren.rocks
  • 772
  • 1
  • 7
  • 22
0

Some of these answers are amazing but didn't work on my Ubuntu VM for some reason. Adding /usr/lib/gcc/x86_64-linux-gnu/11 (in my case 11) to my path worked in my case.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
Yuv
  • 61
  • 6