C++ file including C header file [duplicate] - c++

This question already has answers here:
error: 'INT32_MAX' was not declared in this scope
(10 answers)
Closed 8 years ago.
I need to include a C header file in my C++ project but g++ throws "not declared in this scope" errors.
I read that i need to use extern "C" keyword to fix it but it didn't seem to work for me.
Here is a dummy example triggering this error.
main.cpp:
#include <iostream>
extern "C"
{
#include "includedFile.h"
}
int main()
{
int a = 2;
int b = 1212;
std::cout<< "Hello World!\n";
return 0;
}
includedFile.h
#include <stdint.h>
enum TypeOfEnum {
ONE,
TWO,
THREE,
FOUR = INT32_MAX,
};
The error thrown is :
$> g++ main.cpp
In file included from main.cpp:4:0:
includedFile.h:7:9: error: ‘INT32_MAX’ was not declared in this scope
FOUR = INT32_MAX,
I saw on this post that I may need #define __STDC_LIMIT_MACROS without any success.
Any help is welcome!

<cstdint> is a C++11 feature. Pass -std=c++11 to your compiler.

Related

C++ Compiler finds values defined in header multiple times [duplicate]

This question already has answers here:
Header/Include guards don't work?
(6 answers)
Closed 6 years ago.
im searching the internet for about half an hour because of a (actually basic and simple) problem in C++. Maybe im missing something, but I don't know what. Lets say, i have 3 files: "main.cpp", "dosomething.cpp" and "Header.h".
"Header.h":
#pragma once
#ifndef HEADER_H
#define HEADER_H
char text[] = "This is a text";
#endif // !HEADER_H
"main.cpp"
#include <stdio.h>
#include <iostream>
#include "Header.h"
using namespace std;
void main() {
cout << text << endl;
}
and "dosomething.cpp"
#include "Header.h"
void dosth() {
}
Now the compiler/linker tells me that "text" is already defined in another file. Why? I know how guard idioms such as #pragma once and #ifndef etc. work - atleast I think so. I have no idea whats wrong here. The code itself works (when not including the header in "dosomething.cpp").
Any idea?
Edit: Im using Visual Studio 2015
You need to put extern keyword. C/C++ does for every .c/.cpp file will combine the text of files and all definitions will be merged in the linking step. If you write 'extern' before the header variable, you can define it in just one C++ file, and all other files will reuse it. The linker will use just one instance of the variable you externed it to.
So in header.h
#pragma once
extern char text[];
main.cpp remains the same, but"dosomething.cpp" changes slightly
#include "Header.h"
(...)
char text[] = "...";
(...)
https://en.wikipedia.org/wiki/External_variable

Undefined reference to sample void function [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 6 years ago.
well I want to understand linking with headers and other .cpp (functions for example) so my quastion is why I get "undefined reference to 'afis(). There are sample example and I want to clarify this. Also sorry for my bad english :D.
There is main:
#include <iostream>
#include "functions.h"
using namespace std;
int main()
{
afis();
return 0;
}
There is an function named function.cpp:
#include <iostream>
using namespace std;
void afis(){
cout <<"yehe";
}
And there is the header :
#ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED
void afis();
#endif // FUNCTIONS_H_INCLUDED
While the C++ compiler automatically "pulls in" referenced header files, it can't do that for the actual .cpp code files.
Instead of calling
CXX/clang++/g++ main.cpp -o hello
you need to manually include all relevant code files:
CXX/clang++/g++ main.cpp functions.cpp -o hello

How to define member function in separate source file? [duplicate]

This question already has answers here:
C++ Header Files, Code Separation
(4 answers)
Closed 7 years ago.
Consider following two programs.
p1.cpp
#include <iostream>
struct test
{
void fun();
};
int main()
{
test t;
t.fun();
}
p2.cpp
#include <iostream>
void test::fun()
{
std::cout<<"fun() is called\n";
}
I am compiling like following.
g++ -c -o p1.o p1.cpp
g++ -c -o p2.o p2.cpp <--------- This gives me compiler error.
How can I solve this error? What I am doing wrong?
Essentially, you need to:
Create a new file test.h
Move your struct test { ... }; into the file test.h
Add `#include "test.h" to both of your source files.
Recompile both files.
You may want to consider using a makefile to compile your files, and add a dependency on test.h to p1.cpp and p2.cpp, so that these get recompiled when you modify test.h

Fatal Error in c++ while using local variables [duplicate]

This question already has answers here:
Where to get iostream.h
(3 answers)
Closed 7 years ago.
As am learning cpp tutorial
#include <iostream.h>
using namespace std;
int main()
{
//variable declaration
int a,b;
int c;
//actual initialization
a=10;b=20;
c=a+b;
cout<<c;
return 0;
}
my error
fatal error: iostream.h
Just change <iostream.h> to <iostream>
Reason is that .h header extensions were used for C includes but aren't used for C++ anymore.
In fact, you can actually use C libraries with .h it's just there isn't one for iostream since its C++ exclusive, hence the fatal error.

Problem with using functions in 1 cpp file in another

I have 1 cpp file with main().
It relies on structs and functions in another (say, header.hpp).
The structs are defined in header.hpp, along with function prototypes. The functions are implemented in header.cpp.
When I try to compile, I get an error message saying:
undefined reference to `see_blah(my_thing *)`
So to give an overview:
header.hpp:
#ifndef HEADERDUR_HPP
#define HEADERDUR_HPP
struct my_thing{
int blah;
};
int see_blah(my_thing*);
#endif
header.cpp:
#include "header.hpp"
int see_blah(my_thing * thingy){
// ...
}
main.cpp:
#include <iostream>
#include "header.hpp"
using namespace std;
int main(void)
{
thinger.blah = 123;
cout << see_blah(&thinger) << endl;
return 0;
}
I have no idea what I'm doing wrong, and I can't find any answers. Thanks for any answers, they are very much appreciated!
You should be aware that you're missing a semi-colon at the end of your structure definition. This means it's folding the two (supposedly separate) parts together and that you're not getting the function prototype as a result.
The following compiles fine (after fixing a couple of other errors as well):
// main.cpp
#include <iostream>
#include "header.hpp"
using namespace std; // <- not best practice, but irrelevant here :-)
int main(void)
{
my_thing thinger; // <- need this!
thinger.blah = 123;
cout << see_blah(&thinger) << endl;
return 0;
}
// header.cpp
#include "header.hpp"
int see_blah(my_thing * thingy){
// ...
}
// header.hpp
#ifndef HEADERDUR_HPP
#define HEADERDUR_HPP
struct my_thing{
int blah;
}; // <- see here.
int see_blah(my_thing*);
#endif
with:
g++ -o progname main.cpp header.cpp
gcc actually gave an error with that code you posted so I'm not certain why your compiler didn't. That command line above is also important - if you're compiling and linking in one step, you need to provide all required C++ source files (otherwise the linker won't have access to everything).
Your code is fine. You're just compiling it wrong. Try:
g++ main.cpp header.cpp
You need to:
#include "header.hpp"
in your *main.cpp file.
If you have included header.hpp, than probably you haven't link it(header.cpp) with main.cpp. What environment are you using(g++ or VC++)?
Edit:for linking in g++ you must write:
g++ main.cpp header.cpp -o program
Also you are missing semicolon in the end of your struct!
thinger.blah = 123; should be along the lines of:
my_thing thinger = { 123 };
in addition to issues other posters have mentioned. please, update your example so it compiles.
You are missing a semi colon at the end of your structure definition and mixing it with the method.