Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ GCC - Unable to include ctime, cstring or cstdlib

Tags:

c++

include

gcc

So I have created a simple function to get the current date and time. It works fine when I am testing it in Visual Studio on my Windows machine.

These are the directories included in order for my date time function to work:

#include <ctime>
#include <cstring>
#include <cstdlib>

Adding these to the top of my c++ program in Visual Studio will work fine, but when I try to compile it using GCC on my VPS server, I get this error

main.c:12:17: error: ctime: No such file or directory
main.c:13:19: error: cstring: No such file or directory
main.c:14:19: error: cstdlib: No such file or directory

Here is how I compile my C++ program on my VPS server using GCC:

gcc main.c -o bin/main `mysql_config --cflags --libs` -std=c99 -lpthread;
like image 710
coddding Avatar asked Feb 16 '17 23:02

coddding


3 Answers

gcc is not the compiler for c++. you need to use g++.

See a live example here.

You will also need to review your flags and -std type. You are trying to compile a c++ file as c.

like image 181
Fantastic Mr Fox Avatar answered Oct 13 '22 22:10

Fantastic Mr Fox


gcc is a C compiler. To compile C++ code you need to use g++.

Your source file should also have a .cpp or .cxx extension.

like image 29
user253751 Avatar answered Oct 13 '22 23:10

user253751


Your code is of C++; however, there are 2 mistakes out of your code that are leading to these errrors:

Firstly, your file with the code should be named with a .cpp or .cxx extension at the end of it. Right now, it is main.c, and .c is an extension for a file of C code, not C++. So, you should change your file name to main.cpp.

Secondly, put simply, gcc is a compiler meant for C code. Your code, once again, is C++. Therefore, use g++ in your command, not gcc.

like image 2
BusyProgrammer Avatar answered Oct 14 '22 00:10

BusyProgrammer