0

My program is extremely simple and I'm just learning how to work with prototypes by making header files.

My folder setup is like this:


  • other.cpp
  • other.h
  • entry.cpp

My file, entry.cpp, looks like this:

#include "other.h"
int main(){
   doSomething();
   return 0;
}

doSomething is a function declared in other.cpp and is prototyped in other.h like so:

//ifndefine pre-processor statements
void doSomething();

Building is where I run into trouble. Runningg++ main_c_file.cpp -o name.exe in the console is supposed to create the executable. The problem I am having is that it doesn't know what doSomething is.

I suspect it's because the processor is not searching for other.cpp. I assumed, based on the tutorial I am following, that I didn't actually need to specify this file; it worked fairly well in their video to simply include just the header file and have the cpp file in the same directory.

further: I downloaded their source code and the problem remained. It is not my code, it is how I am trying to build my files. I am not using an IDE to build my programs (I am using Atom IDE to write programs if you know of a shortcut through there), and like stated earlier I am just running the Mingw g++ cppfile.cpp -o command.

Besides the straight answer, if there is a way that can make building cpp projects easier please link me to it or write it here. I have had nothing but trouble in the past trying to learn this language.

Andrew
  • 3,393
  • 4
  • 25
  • 43
  • 2
    g++ entry.cpp other.cpp -o name.exe You have to make sure to compile your other source files, if you have them compiled then you will need to specify their object files. – ChajusSaib Jul 21 '15 at 22:52
  • Thanks for a solution! It works, but if you know of a way to simplify this, or a program that automates this I would appreciate it even more :). – Andrew Jul 21 '15 at 22:55
  • 2
    If the code is in file `a/b/c/d/e/f.cpp`, and you `#include "a/b/c/d/e/f.h"` in `main.cpp`, the compiler simply won't magically search `a/b/c/d/e/f.cpp`. IDEs that provide project building capabilities sometimes hide the fact that you'd use `g++ main.cpp a/b/c/d/e/f.cpp -o my_program` on the command line to build the working program. You could look into using makefiles and download GNU Make if you don't have it already. That way, you'd need only to type `make` to create your program. –  Jul 21 '15 at 22:56
  • @Lemony-Andrew [makefiles?](http://stackoverflow.com/questions/2908057/makefiles-compile-all-cpp-files-in-src-to-os-in-obj-then-link-to-binary) Another link [here](http://stackoverflow.com/questions/2481269/how-to-make-simple-c-makefile) – ChajusSaib Jul 21 '15 at 22:57

0 Answers0