162

When I compile a C/C++ program with popen in PHP, I get this error:

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

But if I run PHP code in the 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; ?> 
9
  • 2
    Have you tried to print env variables and compare them? Do you have safe mode on or off? Commented Jan 16, 2012 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.. Commented Jan 16, 2012 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. Commented Jan 16, 2012 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.. Commented Jan 16, 2012 at 11:02
  • 1
    gcc -print-search-dirs how about this? Are the outputs the same? Commented Jan 16, 2012 at 11:10

11 Answers 11

206

You need to install the gcc-c++ package.

yum install gcc-c++ 
Sign up to request clarification or add additional context in comments.

3 Comments

for alpine, this was apk add g++
for Ubuntu this was apt install g++
for Debian this was also apt install g++
99

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++ 

2 Comments

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.
i have this latest version still cant find+
41

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!

4 Comments

This does not solve the problem, I did not find a way to change these links.
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.
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.
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
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.

1 Comment

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.
4

For APK, the easiest way is:

apk add build-base

Comments

3

Install g++. On openSUSE, run

zypper in gcc-c++ 

Comments

3

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 

Addition:

The GCC and G++ versions should match. You can check the default versions like this:

gcc --version g++ --version 

If the versions don't match, install the latest versions that match with (replace 12 with the desired version):

sudo apt-get install --reinstall gcc-12 sudo apt-get install --reinstall g++-12 

After the installation is done, check the default versions again with the commands above. If the versions of GCC and G++ still don't match, check all available/installed versions with:

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

If there are multiple versions for each package, create a list of compiler alternatives like this:

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 12 sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-12 12 

Then check the available compilers with the commands below. Each one will show a list with options - choose the default version by typing the selection number:

sudo update-alternatives --config gcc sudo update-alternatives --config g++ 

And you're done! Now check the default versions again - they should match!

Source: linuxconfig.org

Comments

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.

Comments

1

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

unset GCC_ROOT 

In more complicated cases, you may need to repoint it to a proper location.

Comments

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.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.