Multi-file C++ compilation -


(hopefully) quick question can't find answer to:

i have been given brief assignment in c++. write 3-file program. there function file, header file, , driver file. here's i've got far:

header (test.h):

#include <iostream> using namespace std; #ifndef test_h #define test_h  int foo (int bar);  #endif 

function (test.cpp):

#include <iostream> #include "test.h" using namespace std;  int foo (int bar){     bar++; } 

driver (drive.cpp):

#include <iostream> #include "test.h" using namespace std;  int main(){     int x = foo(2);     cout << x << endl;     return x; } 

when try compile drive.cpp, following error:

drive.cpp:(.text+0xe): undefined reference `foo(int)' 

so...what doing wrong?

for small project this, compile .cpp files @ once:

g++ main.cpp driver.cpp 

for larger project, separate compile , link steps:

compile:

g++ -c main.cpp -o main.o g++ -c driver.cpp -o driver.o 

link:

g++ main.o driver.o 

or rather, you'd have makefile or ide you.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

c++ - Clear the memory after returning a vector in a function -

erlang - Saving a digraph to mnesia is hindered because of its side-effects -