Author Topic: Linking .cpp and .h  (Read 6026 times)

Offline Lowkus

  • Multiple posting newcomer
  • *
  • Posts: 14
Linking .cpp and .h
« on: July 11, 2016, 09:19:29 am »
Is CodeBlocks supposed to automatically know to link a .cpp file if the matching .h file is #included?

I have a redgloves.h file in the project with this line:
  extern const unsigned char RedGloves_png[5];

And a redgloves.cpp file in the project with this line:
  const unsigned char RedGloves_png[5] = {48,49,50,51,52};

And a main.cpp file in the project with these lines:
  #include "redgloves.h"
  wxMemoryInputStream istream(RedGloves_png, sizeof RedGloves_png);

When I attempt to compile it throws the error "undefined reference to RedGloves_png".  If I cut the line from RedGloves.cpp and put it into the RedGloves.h file, then everything compiles without error.  I don't think I should have to move it out of the RedGloves.cpp file.
« Last Edit: July 11, 2016, 09:42:13 am by Lowkus »

Offline raynebc

  • Almost regular
  • **
  • Posts: 217
Re: Linking .cpp and .h
« Reply #1 on: July 11, 2016, 09:45:46 am »
If you're working with multiple source/header files, create a project containing all of them.


Offline Lowkus

  • Multiple posting newcomer
  • *
  • Posts: 14
Re: Linking .cpp and .h
« Reply #3 on: July 12, 2016, 08:56:25 pm »
The files are all within one project.

Here's the build log:

Build started on: 12-07-2016 at 11:52.08
Build ended on: 12-07-2016 at 11:52.25
-------------- Build: Debug in cshp (compiler: GNU GCC Compiler)---------------
mingw32-g++.exe -Wall -pipe -mthreads -D__GNUWIN32__ -D__WXMSW__ -DwxUSE_UNICODE -g -std=c++14 -IC:\wxWidgets-3.1.0\include -IC:\wxWidgets-3.1.0\lib\gcc_dll\mswu -c C:\Users\Mark\Documents\cProjects\Cshp\cshpApp.cpp -o obj\Debug\cshpApp.o
mingw32-g++.exe -Wall -pipe -mthreads -D__GNUWIN32__ -D__WXMSW__ -DwxUSE_UNICODE -g -std=c++14 -IC:\wxWidgets-3.1.0\include -IC:\wxWidgets-3.1.0\lib\gcc_dll\mswu -c C:\Users\Mark\Documents\cProjects\Cshp\cshpMain.cpp -o obj\Debug\cshpMain.o
mingw32-g++.exe -Wall -pipe -mthreads -D__GNUWIN32__ -D__WXMSW__ -DwxUSE_UNICODE -g -std=c++14 -IC:\wxWidgets-3.1.0\include -IC:\wxWidgets-3.1.0\lib\gcc_dll\mswu -c C:\Users\Mark\Documents\cProjects\Cshp\redgloves.cpp -o obj\Debug\redgloves.o
windres.exe -IC:\wxWidgets-3.1.0\include -IC:\wxWidgets-3.1.0\lib\gcc_dll\mswu -Iresources -J rc -O coff -i C:\Users\Mark\DOCUME~1\CPROJE~1\Cshp\resource.rc -o obj\Debug\resource.res
mingw32-g++.exe -LC:\wxWidgets-3.1.0\lib\gcc_dll -o bin\Debug\CShp.exe obj\Debug\cshpApp.o obj\Debug\cshpMain.o obj\Debug\redgloves.o obj\Debug\resource.res -mthreads -lkernel32 -luser32 -lgdi32 -lwinspool -lcomdlg32 -ladvapi32 -lshell32 -lole32 -loleaut32 -luuid -lcomctl32 -lwsock32 -lodbc32 -lwxmsw31u -mwindows
obj\Debug\cshpMain.o: In function "ZN9cshpFrame14OnButton1ClickER14wxCommandEvent':
C:/Users/Mark/Documents/cProjects/Cshp/cshpMain.cpp:149: undefined reference to "RedGloves_png'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 16 second(s))
2 error(s), 0 warning(s) (0 minute(s), 16 second(s))

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
Re: Linking .cpp and .h
« Reply #4 on: July 12, 2016, 11:14:23 pm »
i can't reproduce this with a minimal example...

if i create a project with the following files:
a.cpp
Code
#include "a.h"

const int test[5] = {42,43,44,45,46};

a.h
Code
extern const int test[5];

main.cpp:
Code
#include <iostream>
#include "a.h"

using namespace std;

int main()
{
    cout << "Hello world!" << endl;
    cout << test[3] << endl;
    return 0;
}

compiling (i made the main.cpp compiling weight lower, so it gets compiled earlier, like in your project:
Code
[ 33.3%] mingw32-g++.exe -Wall -fexceptions -g -std=gnu++11 -Wno-unused-local-typedefs -Wno-deprecated-declarations  -c ###\main.cpp -o obj\Debug\main.o
[ 66.7%] mingw32-g++.exe -Wall -fexceptions -g -std=gnu++11 -Wno-unused-local-typedefs -Wno-deprecated-declarations  -c ###\fghhh\a.cpp -o obj\Debug\a.o
[100.0%] mingw32-g++.exe  -o bin\Debug\fghhh.exe obj\Debug\main.o obj\Debug\a.o   
Output file is bin\Debug\fghhh.exe with size 1.01 MB
Process terminated with status 0 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))
Build log saved as:

all fine,
also your coding style is ok (http://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files-in-c)

so there has to be something else...
can you try to replicate this with a minimal example?

greetings

Offline Lowkus

  • Multiple posting newcomer
  • *
  • Posts: 14
Re: Linking .cpp and .h
« Reply #5 on: July 12, 2016, 11:26:07 pm »
That link helped.  I had #Include "redgloves.h" in the main cpp file, but not in the redgloves.cpp file.  After adding that include to redgloves.cpp the project compiled successfully.  Thanks!