Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot compile simple c++ program in Ubuntu

I tried to build a simple program in the terminal.

#include <stdio.h>
#include <stdlib.h>
int main()
{
        printf("TESTING");
        return 1;
}

I ran g++ -o test test.cpp

The errors:

/usr/include/features.h:323:26: error: bits/predefs.h: No such file or directory
/usr/include/features.h:356:25: error: sys/cdefs.h: No such file or directory
/usr/include/features.h:388:23: error: gnu/stubs.h: No such file or directory
In file included from test.cpp:2:
/usr/include/stdlib.h:42:29: error: bits/waitflags.h: No such file or directory
/usr/include/stdlib.h:43:30: error: bits/waitstatus.h: No such file or directory
/usr/include/stdlib.h:320:49: error: sys/types.h: No such file or directory
In file included from test.cpp:2:
/usr/include/stdlib.h:35: error: ‘__BEGIN_DECLS’ does not name a type
/usr/include/stdlib.h:102: error: expected constructor, destructor, or type conversion  before ‘;’ token
/usr/include/stdlib.h:113: error: ‘__END_NAMESPACE_STD’ does not name a type
/usr/include/stdlib.h:122: error: expected constructor, destructor, or type conversion before ‘;’ token
/usr/include/stdlib.h:140: error: expected constructor, destructor, or type conversion before ‘extern’
/usr/include/stdlib.h:145: error: expected constructor, destructor, or type conversion before ‘extern’
/usr/include/stdlib.h:149: error: expected initializer before ‘__THROW’
/usr/include/stdlib.h:152: error: expected initializer before ‘__THROW’
/usr/include/stdlib.h:153: error: ‘__END_NAMESPACE_STD’ does not name a type
/usr/include/stdlib.h:160: error: ‘__END_NAMESPACE_C99’ does not name a type
/usr/include/stdlib.h:168: error: ‘__END_NAMESPACE_STD’ does not name a type

The list continues this way. Im hoping someone can point out what I haven't done to make this work.

like image 555
Nick Schudlo Avatar asked Feb 15 '12 23:02

Nick Schudlo


People also ask

Does Ubuntu have C compiler?

Linux is a set of open-source UNIX-like operating systems, and Ubuntu is a Linux-based operating system commonly used to run Linux-based applications. To install C on Linux and to build and run our C program file on Ubuntu, we need to install the GCC Compiler.


1 Answers

Your code works for me with the same platform.

The error messages look like C errors. Perhaps using the C++ headers will help.

#include <cstdio>
#include <cstdlib>

int main(int argc, char *argv[]) {
  printf("TESTING");
  return 0;
}

You may also have some weird aliases. Sometimes people setup gcc as an alias for g++ incorrectly.

tom@flim:~$ set | grep g++

tom@flim:~$ alias grep
alias grep='grep --color=auto'

tom@flim:~$ alias g++
bash: alias: g++: not found

tom@flim:~$ which g++
/usr/bin/g++

tom@flim:~$ ll `which g++`
lrwxrwxrwx 1 root root 7 2011-08-14 02:17 /usr/bin/g++ -> g++-4.6*

tom@flim:~$ g++ --version
g++ (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
Copyright (C) 2011 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.

This is how I setup my dev environment in ubuntu:

sudo apt-get install build-essential

This sets up all the standard C++ libraries without needing to know the knitty gritty details.

like image 102
Tom Kerr Avatar answered Sep 17 '22 06:09

Tom Kerr