0

how do I compile a .c or .cpp file using GCC? I need to include some standard libraries (fstream, string, iostream) - how do I do this?

For clarification, here:

#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include"ocgenerator.h";
#include"structures.h";
#include"util.h";
#include"util2.h";
using namespace std;

(my .h files are in the same directory as the src file) If I use the command: gcc src.cpp -o src.o

I get a lot of errors: memcpy, atoi, atol, strncmp, etc, ... "are not declared in this scope". What should I add to the command?

edit: Or is it a scope thing, and do I have to add std:: to all those functions?

Alek988Alek
  • 49
  • 14

3 Answers3

1

memcpy and strncmp are declared in <cstring>, atoi and atol in <cstdlib>. Just include these headers to bring in their declarations.

Side notes :

  • No semicolon after preprocessor directives, including #include "".
  • No using namespace std;, especially not in headers !
    (Why is "using namespace std" considered bad practice?)
  • Since you're building a C++ project, don't forget to link with the standard library via -lstdc++, or use g++ which forwards it to the linker for you.

Note that with GCC you don't have to prefix standard C functions with std:: (as they are also declared in the global namespace), but you should anyway for your code to be standard-compliant.

Community
  • 1
  • 1
Quentin
  • 62,093
  • 7
  • 131
  • 191
0

The cplusplus site helps out. If you search for a specific function, on the top you will find the necessary references for the code to compile:

http://www.cplusplus.com/reference/cstdlib/atol/

You can even check in the URL. In this case, you need 'cstdlib'.

Regarding the compiler, there are several available, g++ is a good option.

I also suggest creating a makefile for automating the building process, because it becomes an headache when your codebase grows.

Miguel Mesquita Alfaiate
  • 2,851
  • 5
  • 30
  • 56
0

For any library function you use in your code, read the man page for that function to see which header declares it, and #include that header in any source file that uses the function.

For example, man memcpy shows the memcpy(3) man page (the (3) refers to section 3 of the manual; use man 3 memcpy to specify the section).

NAME
memcpy - copy memory area

SYNOPSIS
#include <string.h>

void *memcpy(void *dest, const void *src, size_t n);
...

memcpy is part of the C standard library, and is declared in <string.h>. For C++ you can use <string.h>, but it's probably better to use <cstring>, which puts the function name in the std namespace. In general, each standard C header <foo.h> is duplicated as a C++ header <cfoo>.

I get a lot of errors: memcpy, atoi, atol, strncmp, etc,

memcpy and strncmp are declared in <cstring>. atoi and atol are <cstdlib> (or in <string.h> and <stdlib.h> if you're programming in C rather than in C++).

And be sure to use the g++ command, not the gcc command, to compile C++ source. They both invoke the same compiler (gcc compiles C++ code if the source file name ends in .cpp), but the g++ command adds some options that are needed for C++.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631