0

I have created two basic .cpp files. First one has just one line of code: int var=10; , and the second one is this:

#include <iostream>

using namespace std; 
           
int main(){

    cout<<var;
                  
    return 0; 

}             

I compile my code using gcc -o myprogram file1.cpp file2.cpp & I always get this message:

error: ‘var’ was not declared in this scope
   13 |     cout<<var;

Does anyone know how to solve this?

Miles Budnek
  • 28,216
  • 2
  • 35
  • 52
  • 3
    Welcome to SO! How about `extern int var` in the `main` file? This seems unrelated to vim. Also, don't use [`using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) and probably use `g++` instead of `gcc`. – ggorlen Dec 24 '20 at 22:20
  • @ggorlen Now I get this message: collect2: error: ld returned 1 exit status –  Dec 24 '20 at 22:23
  • 1
    Works fine for me so your code or compiler command must be different than what you showed here. – ggorlen Dec 24 '20 at 22:24
  • @ggorlen so how do I run code using g++, I was able to compile it but idk how to run it. (I'm a new to programming, so sry if it's a dumb question lol) –  Dec 24 '20 at 22:42
  • 1
    No problem, we were all new at some point. Take the file you've shown as it is, add the `extern int var;` above `int main`, then compile and run with `g++ -o myprogram learn.cpp help.cpp && ./myprogram`. `g++` is a compiler, not a program runner. It creates an executable binary from your source code. What OS are you on? Also, pick up a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), any beginner-level one will walk you through all of this. – ggorlen Dec 24 '20 at 22:45
  • @ggorlen linux, ubuntu if that matters –  Dec 24 '20 at 22:46
  • Good, me too. The above commands should work fine. Let us know how it goes. – ggorlen Dec 24 '20 at 22:47
  • 1
    @ggorlen thanks it worked –  Dec 24 '20 at 22:49
  • You're trying to compile C++ with a C compiler. That's not going to work. – xaxxon Dec 24 '20 at 23:09
  • apparently this question was significantly edited - it needs to be closed. The answers don't match the question anymore. – xaxxon Dec 24 '20 at 23:50
  • @bb_823 - Please "upvote" and "accept" my reply (with the subsequent thread) if you found it useful. Or, if you prefer, "upvote" my answer and "accept" your reply. – paulsm4 Dec 24 '20 at 23:53
  • 2
    @bb_823 I've rolled your question back to the original question. In the future please refrain from editing your questions in a way that invalidates existing answers. You should ask a new question in those situations instead. – Miles Budnek Dec 25 '20 at 01:06

3 Answers3

2

If "var" is a one-off, then simply add an extern:

#include <iostream>

using namespace std;

extern int var; 
           
int main(){
    cout<<var;    
    return 0; 
}

If your "first .cpp" grows, and you start adding things other modules will want to use, then you'll probably want to:

  • Define one or more classes
  • Create a header file for your class definition(s)
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • What if it ain't a one-off? – JVApen Dec 24 '20 at 22:22
  • I've tried that and I get this message: ```/usr/bin/ld: /tmp/ccAQZ8HD.o: in function `main': file2.cpp:(.text+0x13): undefined reference to `std::cout' /usr/bin/ld: file2.cpp:(.text+0x18): undefined reference to `std::ostream::operator<<(int)' /usr/bin/ld: /tmp/ccAQZ8HD.o: in function `__static_initialization_and_destruction_0(int, int)': file2.cpp:(.text+0x4c): undefined reference to `std::ios_base::Init::Init()' /usr/bin/ld: file2.cpp:(.text+0x61): undefined reference to `std::ios_base::Init::~Init()' collect2: error: ld returned 1 exit status``` –  Dec 24 '20 at 22:25
  • 1
    @bb_823: the code you posted (without "extern') will give you the compile error `‘var’ was not declared in this scope`. If you've changed anything, please "Edit" your post: 1) Add the new, modified code, 2) Copy/paste the *complete* text of the new error message – paulsm4 Dec 24 '20 at 22:27
  • 2
    @bb_823: Use `g++` to compile C++ programs. Just adding a .cpp isn't sufficient :( "gcc" is for "C" programs only; "g++" can compile either C (.c) or C++ (.cpp) source files. – paulsm4 Dec 24 '20 at 22:28
  • @paulsm4 so how do I run code using g++, I was able to compile it but idk how to run it. (I'm a new to programming, so sry if it's a dumb question lol) –  Dec 24 '20 at 22:43
  • Q: so how do I run code using g++? A: You don't. You just *build* code (into an executable program) with g++. You can then run it as you would any other program :) Since you're on Linux, I'd type `g++ -Wall -g file1.cpp file2.cpp -o myapp` to build, then `./myapp` (whatever your program's named) from a terminal window to run it. – paulsm4 Dec 24 '20 at 22:54
  • How does this fix that he's using the wrong compiler? – xaxxon Dec 24 '20 at 23:10
  • @xaxxon - the OP edited his post; he deleted the 1st problem (compile error because of missing "extern") and added a different, second problem (link error, using the wrong compiler). – paulsm4 Dec 24 '20 at 23:34
1

Every translation unit must contain a declaration of every symbol it uses. On the other hand, every symbol must have a definition in exactly one translation unit (unless it's explicitly or implicitly inline; then multiple translation units can have definitions as long as they're all the same).

The usual way to do this is to put the declarations for things in a header file, which you then #include into multiple translation units, while putting their definitions in a .cpp file that forms the main body of the translation unit.


In this particular case, you need a declaration of var in the translation unit that contains main. You could put it in a header that gets #included into file1.cpp; i.e.:

file2.hpp:

#ifndef FILE1_HPP
#define FILE1_HPP

extern int var;

#endif

file2.cpp:

#include "file2.hpp"

int var;

file1.cpp:

#include "file2.hpp"
#include <iostream>

int main() {
    std::cout << var;
}

Or you just put it into that translation unit manually; i.e.

#include <iostream>

extern int var;

int main()
{
    std::cout << var;
}

Either approach is exactly the same from the compiler's point of view (the preprocessor just does text replacement), but the latter tends to be much more difficult for humans to deal with, so you should probably avoid it in general.

paulsm4
  • 114,292
  • 17
  • 138
  • 190
Miles Budnek
  • 28,216
  • 2
  • 35
  • 52
1

Basically the problem was that I used gcc -o myprogram learn.cpp help.cpp instead of g++ -o myprogram learn.cpp help.cpp to compile. Gcc compiler is used for .c files and g++ for .cpp files

xaxxon
  • 19,189
  • 5
  • 50
  • 80