Cannot compile Classes example in ofBook

Hello, I’m very new to openFrameworks. I’m used to working in Processing, and have been working through the ofBook to get up to speed with the idiosyncracies of openFrameworks. I made it through the Basics chapter fine butm now that I’m writing classesm the examples are failing to compile. I copied and pasted the code exactly from the first example into my files after a few attempts, and it is still failing. Cannot figure why I keep getting this error:
Undefined symbols for architecture x86_64: "Ball::Ball()", referenced from: ofApp::ofApp() in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

Again, this is my first time using Xcode or oF, so I have no idea what any of that means. Apologies if this is a very amateur mistake.

I have looked at many other forum and stackoverflow posts that have the same error, but the solutions there were either nonapplicable or did not work. Any advice on this would be greatly appreciated!

Here are my header and cpp files for the Ball class:
Ball.hpp:


#ifndef Ball_hpp
#define Ball_hpp

#include <stdio.h>
#include "ofMain.h" // we need to include this to have a reference to the openFrameworks framework
class Ball {

    public: // place public functions or variables declarations here

    // methods, equivalent to specific functions of your class objects
    void setup();    // setup method, use this to setup your object's initial state
    void update();  // update method, used to refresh your objects properties
    void draw();    // draw method, this where you'll do the object's drawing

    // variables
    float x;        // position
    float y;
    float speedY;   // speed and direction
    float speedX;
    int dim;        // size
    ofColor color;  // color using ofColor type

    Ball();  // constructor - used to initialize an object, if no properties are passed the program sets them to the default value
}; // don't forget the semicolon!
#endif /* Ball_hpp */

Ball.cpp:

#include "Ball.hpp"
Ball::Ball(){
}

void Ball::setup(){
    x = ofRandom(0, ofGetWidth());      // give some random positioning
    y = ofRandom(0, ofGetHeight());

    speedX = ofRandom(-1, 1);           // and random speed and direction
    speedY = ofRandom(-1, 1);

    dim = 20;

    color.set(ofRandom(255),ofRandom(255),ofRandom(255)); // one way of defining digital color is by addressing its 3 components individually (Red, Green, Blue) in a value from 0-255, in this example we're setting each to a random value
}

void Ball::update(){
    if(x < 0 ){
        x = 0;
        speedX *= -1;
    } else if(x > ofGetWidth()){
        x = ofGetWidth();
        speedX *= -1;
    }

    if(y < 0 ){
        y = 0;
        speedY *= -1;
    } else if(y > ofGetHeight()){
        y = ofGetHeight();
        speedY *= -1;
    }

    x+=speedX;
    y+=speedY;
}

void Ball::draw(){
    ofSetColor(color);
    ofDrawCircle(x, y, dim);
}

And here’s ofApp.h and .cpp:

#pragma once

#include "ofMain.h"
#include "Ball.hpp" //include the header file for the Ball class

class ofApp : public ofBaseApp{

	public:
   
		void setup();
		void update();
		void draw();

		void keyPressed(int key);
		void keyReleased(int key);
		void mouseMoved(int x, int y );
		void mouseDragged(int x, int y, int button);
		void mousePressed(int x, int y, int button);
		void mouseReleased(int x, int y, int button);
		void mouseEntered(int x, int y);
		void mouseExited(int x, int y);
		void windowResized(int w, int h);
		void dragEvent(ofDragInfo dragInfo);
		void gotMessage(ofMessage msg);
    
    
    Ball myBall;
};

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
    myBall.setup();//call the setup function inside of the Ball class
}

//--------------------------------------------------------------
void ofApp::update(){
    myBall.update(); //call Ball's update() function
}

//--------------------------------------------------------------
void ofApp::draw(){
    myBall.draw();//call Ball's draw function

}

//--------------------------------------------------------------
void ofApp::keyPressed(int key){

}

//--------------------------------------------------------------
void ofApp::keyReleased(int key){

}

//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){

}

//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){

}

//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){

}

//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){

}

//--------------------------------------------------------------
void ofApp::mouseEntered(int x, int y){

}

//--------------------------------------------------------------
void ofApp::mouseExited(int x, int y){

}

//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){

}

//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){

}

//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){ 

}

it’s giving you a linking error, which means the cpp code wasn’t compiled. Can you double check that the ball.cpp is setup to be part of the target?

for example, if you highlight that file on the left side, and look at the right sidebar (it may be hidden, you may have to toggle it), does it appear to be part of the target?

Screen Shot 2022-02-07 at 12.06.45 PM

linking errors happen when a cpp file (or lib file) is missing, all the code you wrote is compiling fine but the moment the linker goes to put all the compiled objects together its missing that piece

2 Likes

Build succeeded! That fixed it, thanks very much.
I still don’t really get what the Target is. Is Target Membership something I’ll have to assign for every new .cpp file?
Thanks again.

target is basically saying, what project is this file you are making being compiled for. Different projects have multiple targets or sometimes you may include source code that doesn’t have a target (in the case of OF apps there’s usually one target, the app itself) . It often happens to make that I forget to select the target when I drag and drop code into Xcode or create a new class from the interface, so I usually need to double check the target if I see a linking error.

1 Like