Noob Question: why do my header files behave strangely?

62 views
Skip to first unread message

Jack Arrington

unread,
Feb 11, 2014, 1:55:01 PM2/11/14
to emscripte...@googlegroups.com
First I should say that I am far from a C++ ninja, and I've only compiled a few simple things with emscripten thus far.

So I have my main.cpp file, and a class called Foo in Foo.h and Foo.cpp. main.cpp includes Foo.h and Foo.cpp includes Foo.h, but when I compile, I get the error: "warning: unresolved symbol: _ZN5FooC1EV". My understanding was that this was the standard way of doing things in C++. However, when I switch the contents of Foo.h and Foo.cpp to a function declaration and implementation, respectively, I get no such error. When I change the contents of my Makefile from "emcc -g4 src/main.cpp -o out/main.js" to "emcc -g4 src/main.cpp src/Foo.cpp -o out/main.js", everything works. This makes sense to me, since Foo.cpp is not actually getting included anywhere in the code. But what confuses me is that all the emscripten examples I have read do not seem to do this, and code like this works with cl(Microsoft's C++ compiler).

Did I miss something here? Am I making a dumb mistake? Any help is appreciated.(below is posted my code)

Main.cpp:
  #include <iostream>
  #include <stdio.h>
  #include "Foo.h"

  int main(int argc, char const *argv[])
  {
    Foo f;
    printf("%i\n", f.bar);

    return 0;
  }

Foo.h:
  #ifndef FOOH
  #define FOOH
  #pragma once

  class Foo
  {
  public:
    Foo();
    ~Foo();
    
    int bar;
  };

  #endif

Foo.cpp:
   #include "Foo.h"

  Foo::Foo()
  {
    printf("Hello\n");
    bar = 12;
  }

  Foo::~Foo()
  {

  }

Makefile:
   emcc -g4 src/main.cpp src/Foo.cpp -o out/main.js

Jukka Jylänki

unread,
Feb 11, 2014, 2:28:17 PM2/11/14
to emscripte...@googlegroups.com
The .cpp files are never included by other files (well you could, but by good organization practices they don't). They are the 'starting points' of compilation units, which go on to include headers. Each .cpp file is compiled separately as their own compilation unit, and the results are linked together. You must have somehow confused the examples you looked at - all C++ compilers do this: you pass all the .cpp files to the compiler either via a single "cc a.cpp b.cpp c.cpp -o a.out" directive, or via separate directives that create object files "cc a.cpp -o a.obj;cc b.cpp -o b.obj;cc c.cpp -o c.obj" and then finally combine them together with "cc a.obj b.obj c.obj -o a.out". Visual Studio traditionally uses the suffix .obj for intermediate object files, GCC uses .o, emcc can use either. It is also possible to make more complex build hierarchies by compiling multiple object files to static libraries, and then finally combine the individual static libraries to produce the final executable. Perhaps the examples you were looking at all built individual object files, and therefore only ever called emcc with a single .cpp file at a time?


--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Jack Arrington

unread,
Feb 11, 2014, 3:55:30 PM2/11/14
to emscripte...@googlegroups.com
Very informative answer. I looked back on the examples, and it seems they were building individual object files, as you say. This cleared up my confusion, thanks a million!
You received this message because you are subscribed to a topic in the Google Groups "emscripten-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/emscripten-discuss/MeT19yBPseM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to emscripten-disc...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.

No virus found in this message.
Checked by AVG - www.avg.com
Version: 2014.0.4259 / Virus Database: 3697/7084 - Release Date: 02/11/14


Reply all
Reply to author
Forward
0 new messages