For clearly, please view my sample
I have two files: main.cpp and myfunction.h
This is main.cpp
#include <setjmp.h>
#include <myfunction.h>
int main()
{
if ( ! setjmp(bufJum) ) {
printf("1");
func();
} else {
printf("2");
}
return 0;
}
This is myfunction.h
#include <setjmp.h>
static jmp_buf bufJum;
int func(){
longjum(bufJum, 1);
}
Now, I want my screen print "1" and then print "2", but this code is uncorrect!
Please, help me!
Thank you so much!
If you want to have it in multiple files, then you need to create two source files, not a single source file and a header file
myfunction.cpp:
#include <setjmp.h>
extern jmp_buf bufJum; // Note: `extern` to tell the compiler it's defined in another file
void func()
{
longjmp(bufJum, 1);
}
main.cpp:
#include <iostream>
#include <setjmp.h>
jmp_buf bufJum; // Note: no `static`
void func(); // Function prototype, so the compiler know about it
int main()
{
if (setjmp(bufJum) == 0)
{
std::cout << "1\n";
func();
}
else
{
std::cout << "2\n";
}
return 0;
}
If you are using GCC to compile these files, you can e.g. use this command line:
$ g++ -Wall main.cpp myfunction.cpp -o myprogram
Now you have an executable program called myprogram which is made from two files.
I don't know anything about setjmp, but you have at least one mistake in your code:
-#include <myfunction.h>
+#include "myfunction.h"
You have a non-inline function defined in a .h file. While not illegal, this is pretty much always wrong.
You have a static global variable defined in a .h file. While not illegal, this is pretty much always wrong.
Related
I want to create a function outside the main.cpp file
i've tried creating a header file but it doesn't work:
Main.cpp:
#include "other.h"
int main() {
MyFunc();
}
Other.cpp
#include <iostream>
#include "other.h"
void MyFunc() {
std::cout << "Ohai from another .cpp file!";
std::cin.get();
}
Other.h
#include <iostream>
#include "other.cpp"
void MyFunc();
nor CPP, G++, GCC compiler work
GCC Compiling error
Errors shown by vs code
You must include a header file and not a C++ file.
And therefore, you need to remove:
#include "other.cpp"
from other.h & use the following command-line for compiling:
g++ -o output main.cpp other.cpp
You'll get it linked and then compiled, then everything should be working fine.
You must remove #include "other.cpp" in header file.
erase the line "#include "other.cpp" in your other.h and you will be fine...
Edit: you also need a header guard...
I have two headers with source files, lets say file1.h, file1.cc and file2.h, file2.cc. They use each others functions, for example:
file1.h:
void test1();
file2.h:
void test2();
file1.cc:
#include "file1.h"
#include "file2.h"
void test1() {
do_something();
test2();
}
file2.cc:
#include "file1.h"
#include "file2.h"
void test2() {
do_something_else();
test1();
}
I get the problem, the dependancy is mutual and we cant compile one file without having another compiled. How to solve this problem?
Modify the files to remove the double recursion.
Use some code to prevent double inclusion of your .h files (I have used #pragma once at the top of the .h files)
optionally, add a main.cpp to call the functions
provide code for do_something()
compile using . g++ *.cpp -o main
Great question!
Here are the files:
// main.cpp
//
#include <iostream>
#include "file1.h"
#include "file2.h"
int main() {
test2();
std::cout << "Hello, World!" << std::endl;
return 0;
}
// file1.cpp
#include "file1.h"
#include "file2.h"
#include <iostream>
void do_something() {
std::cout << "Just hit do_something" << std::endl;
}
void test1() {
do_something();
// test2(); // Do not use double recursion.
}
// file1.h
#pragma once
void do_something();
void test1();
// file2.cpp
#include "file1.h"
#include "file2.h"
void test2() {
do_something();
test1();
}
// file2.h
#pragma once
void test2();
The best answer is don't design your code like that! This introduces a "circular dependency," which immediately makes your code fragile. Additionally, the way you have your example written describes a double-recursive function (A calls B which calls A), and since there is no terminating condition, is unbounded! As you can see, this can lead to some very unmanageable circumstances.
Let's forego good engineering practices for a while. How you described both headers & source files is adequate and can be built with the following command:
g++ file1.cc file2.cc -o libCoupledRecursiveExample.so
This is where things become difficult: through the build instructions alone, you can't actually detect the dependency! Good static code checkers can detect these circumstances and warn you, but when you use a lazy compiler (like g++ without flags), it's easy to sneak by code like this that won't generate any warnings.
I have this simple program which tries to print my global variable in a separate file. I'm using the Visual Studio 2013 professional IDE.
print.h
#ifndef PRINT_H_
#define PRINT_H_
void Print();
#endif
print.cpp
#include "print.h"
#include <iostream>
void Print()
{
std::cout << g_x << '\n';
}
source.cpp
#include <iostream>
#include <limits>
#include "print.h"
extern int g_x = 5;
int main()
{
Print();
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin.get();
return 0;
}
I get a compiler error error C2065: 'g_x' : undeclared identifier.
I've searched through this forum and was unable to find anyone else having my problem. I've tried re-declaring my global variable in the separate .cpp file with no success. As you can see, I've included the necessary header guards and assigned my global variable the extern keyword. This is my first time testing global variables in multiple files. Obviously I'm missing something simple. What do I need to change or add to make my program work?
EDIT: I found this topic useful in understanding the difference between extern and the definition of a global variable.
The compiler is compiling print.cpp. It knows nothing about source.cpp while it is compiling print.cpp. Therefore that g_x that you placed in source.cpp does you absolutely no good when print.cpp is being compiled, that's why you get the error.
What you probably want to do is
1) place extern int g_x; inside of print.h. Then the compiler will see g_x when compiling print.cpp.
2) in source.cpp, remove the extern from the declaration of g_x:
int g_x = 5;
Move your global declaration to a common header, like common.h:
#ifndef COMMON_H_
#define COMMON_H_
extern int g_x; //tells the compiler that g_x exists somewhere
#endif
print.cpp:
#include <iostream>
#include "print.h"
#include "common.h"
void Print()
{
std::cout << g_x << '\n';
}
source.cpp:
#include <iostream>
#include <limits>
#include "print.h"
#include "common.h"
int g_x;
int main()
{
g_x = 5; //initialize global var
Print();
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin.get();
return 0;
}
In other .cpp files, you can access g_x including the common.h header.
extern int g_x;
belongs to .h, and you need to add
int g_x =5;
to some of .cpp.
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.
What is the correct syntax for having function implementation in a separate file? For example:
foo.h
int Multiply(const int Number);
foo.cpp
#include "foo.h"
int Multiply(const int Number)
{
return Number * 2;
}
I see this used a lot, but when I try it I get an error having to do with a missing main() function. I get the error even when I try to compile working code.
Roughly speaking, you need to have a main() function inside one of your C++ files you are compiling.
As the compiler says, you just need to have a main() method inside your foo.cpp, like so:
#include "foo.h"
#include <iostream>
using namespace std;
int Multiply(const int Number)
{
return Number * 2;
}
int main() {
// your "main" program implementation goes here
cout << Multiply(3) << endl;
return 0;
}
Or you could separate your main function into a different file, like so (omit the main() block in foo.cpp if you intend to do this):
main.cpp
#include "foo.h"
#include <iostream>
using namespace std;
int main() {
cout << Multiply(3) << endl;
return 0;
}
Then compile it like
g++ main.cpp foo.cpp
Every program in C++ is a collection of one or more translation units, aka source files.
After these files get compiled, the linker searches for the entry point of your program aka the int main() function. Since it fails to find it it gives you an error.
Don't forget that building the program is yielding an executable file. An executable file without an entry point is nonsense.