-1

Sorry for the simple question. I am attempting to learn more c++ at a fundamental level. I have always used VS in the past, and I am trying to learn the command line and compile, navigate, etc. with it.

I started with "hello world" and was able to compile it with gcc/clang, then run it with the expected results.

I then slightly reworked this and made a new header/cpp file to do the output part of hello world, and then call that from the main function, described below:

main.cpp:

#include "MyClass.h"

int main(){
    foo();
    return 0;
}

MyClass.h

#pragma once

void foo();

MyClass.cpp

#include "MyClass.h"
#include <iostream>

void foo(){
    std::cout << "Hello World\n";
}

I then have tried to compile with gcc and clang as follows:

clang -Wall -g main.cpp MyClass.cpp

I have tried the same with GCC, and have also tried various invocations of this, such as using -c:

clang -Wall -g -c main.cpp
clang -Wall -g -c MyClass.cpp

Each and every time, I get an error

λ clang -Wall -g MyClass.cpp main.cpp
main.cpp:13:1: error: use of undeclared identifier 'foo'
foo();
^
1 error generated.

I get this same error whether using gcc or clang.

I also tried from scratch on my laptop, to see if there was some more global issue, but I still get the same problem.

I have also tried on the basic Windows command line as well.

Other areas on StackOverflow demonstrate simple ways of compiling multiple files from the command line, and I have tried as they show, but still get errors.

I also know that "make" is something I need to learn as well, however, I just want to make sure I understand what my make file is doing before I dive into that.

I feel like it must be something trivial that I just cannot figure out.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • The code and 1st clang command-line shown look fine to me. – Remy Lebeau Jul 20 '21 at 22:34
  • Are you sure you saved the file from your text editor? I've forgotten that a few times ... – o11c Jul 20 '21 at 22:35
  • I double checked to make sure I am saving, still same issue. I also tried reverting it to the simpler form, no luck... all files are in same folder – ProofOfGravity Jul 20 '21 at 22:36
  • 2
    If the compiler doesn't know what `foo()` is when it is compiling `main.cpp`, then `main.cpp` must not be including the `MyClass.h` file you think it is. You can double-check that by temporarily inserting a line like `#error hey_dude` into your `MyClass.h` file and then recompiling `main.cpp`; if your new `hey_dude` error doesn't show up in the compile, that's a pretty good sign that some other `MyClass.h` file is being included instead. – Jeremy Friesner Jul 20 '21 at 22:37
  • That is very clever Jeremy, I will attempt that – ProofOfGravity Jul 20 '21 at 22:39
  • 1
    The error message seems to be referring to line 13 of `main.cpp`, but the version of that file that you are showing us only has 6 lines. Is the code that you are showing us your actual code? – Andreas Wenzel Jul 20 '21 at 22:39
  • yes, the code is what is written now. I re-wrote it slightly to change some names for posting. – ProofOfGravity Jul 20 '21 at 22:42
  • It works fine for me with `g++`. I don't have clang. – luther Jul 20 '21 at 22:42
  • running ```clang++ -std=c++11 -Wall -g main.cpp MyClass.cpp``` then ```./a.out``` worked for me – NeonFire Jul 20 '21 at 22:43
  • I also just tried the #error suggestion, and in a bizzare twist, it DOES show me the #error I insert when I try to compile. So that leads me to believe that the .h file is being included. With #error commented out, still get the "undefined reference" error as before – ProofOfGravity Jul 20 '21 at 22:43
  • thank you for the test compile, it is great news it works on other systems, I thought I was loosing my mind – ProofOfGravity Jul 20 '21 at 22:44
  • You may want to take a look at the [preprocessor output](https://stackoverflow.com/q/3742822/12149471) to see what your code looks like after the `#include` directives are executed. – Andreas Wenzel Jul 20 '21 at 22:47
  • Ah, thank you Andreas, I did not even think about this, I will try to dig through it now, thank you – ProofOfGravity Jul 20 '21 at 22:52
  • 1
    @ProofOfGravity: "still get the undefined reference error as before" But before you got "undeclared identifier", which is not at all the same as "undefined reference". – Ben Voigt Jul 20 '21 at 23:01

2 Answers2

0

Thank you to Andreas for the suggestion of looking at the preprocessor output. And thank you to everyone for the suggestions.

The pre-processor output did not make sense to what I was compiling.

I was using VSCode, in this case, as a text editor, making brand new files in my folder after launching it from the command line. I thought the files I created in VSCode directly into the folder (named main.cpp, for example), would produce a regular text file. However, for some reason, it did not.

Essentially, I recreated the above program in notepad and was easily able to compile it using the commands I used above. I guess VSCode may not be perfect for me as a pure text editor or I should figure out if there are settings to change to accomplish my goal.

Thank you all again for your time and consideration.

  • `The pre-processor output did not make sense to what I was compiling.` -- Most of the output you see will probably be the contents of the files that you included with the `#include` directives. In the case of `#include `, this will probably be a lot of code. You should look at the end of the output to see your code, or, in the case of `MyClass.cpp`, where `#include ` is put in the middle, you should look at the start and the end of the output, but ignore the middle. You may want to put `#include ` before `#include "MyClass.h"` to avoid this problem. – Andreas Wenzel Jul 20 '21 at 23:00
-3

Use extern on your function. Also make sure you're compiling with c++ and not c; i.e. g++.

MyClass.h

#pragma once

#ifdef __cplusplus
extern "C" {
#endif

extern void foo();

#ifdef __cplusplus
}
#endif
user2205930
  • 1,046
  • 12
  • 26
  • 1
    `extern` is implicit for function declarations; it only matters for variable declarations (and it would result in a multiple-definition error) – o11c Jul 20 '21 at 22:58