News:

Accounts with zero posts and zero activity during the last months will be deleted periodically to fight SPAM!

Main Menu

Linking .cpp and .h

Started by Lowkus, July 11, 2016, 09:19:29 AM

Previous topic - Next topic

Lowkus

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.

raynebc

If you're working with multiple source/header files, create a project containing all of them.


Lowkus

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))

BlueHazzard

i can't reproduce this with a minimal example...

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

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


a.h

extern const int test[5];


main.cpp:
#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:
[ 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

Lowkus

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!