Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9078355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:36:04+00:00 2026-06-16T19:36:04+00:00

I’ve been trying to get used to c++ with cmake and gtest (google test).

  • 0

I’ve been trying to get used to c++ with cmake and gtest (google test).

I’m trying to understand why this setup yields the error

Undefined symbols for architecture x86_64:
"Project1::foo(int&)", referenced from:
    Project1Test_MethodBarDoesAbc_Test::TestBody()      in test_project1.cpp.o
        ld: symbol(s) not found for architecture x86_64

when I run “make”.

When I implement methods in the .h file, things work fine; but when I only declare methods in the .h file and then implement it in the associated .cpp file, this error occurs.

My test file: test_project1.cpp

#include <iostream>
#include "gtest/gtest.h"

#include "project1.h"

// tests outside of the class
TEST(IndependentMethod, ResetsToZero2) {
    int i = 0;
    independentMethod(i);
    EXPECT_EQ(0, i);
}
//...

// The fixture for testing the class
class Project1Test : public ::testing::Test {
protected:
    // You can remove any or all of the following functions if its body
    // is empty.

    Project1Test() {
        // nothing here
    }
};

// Test case must be called the class above
TEST_F(Project1Test, MethodBarDoesAbc) {
    Project1 p;
    int i = 0;
    p.foo(i); // WHY CAN'T Project1::foo(int&) be detected here?!
    EXPECT_EQ(1, i);
}

Class definition: project1.h

#ifndef PROJECT1_H_
#define PROJECT1_H_

#include <iostream> // IO access

using namespace std;

class Project1 {

public:
    // why only detected if implemented here?
    // void foo(int &i) {
    //  i = 1;
    // }
    void foo(int &i);

};

void independentMethod(int &i) {
    // From experience, should be implemented in the .h, not the .cpp.
    // Otherwise, test can't find independentMethod, but WHY?
    i = 0;
}

#endif /* PROJECT1_H_ */

Implementation of Project1 class: project1.cpp

#include <iostream>
#include "project1.h"

void Project1::foo(int &i) {
    i = 1;
}

int main() {
    // this works fine if I directly compile project1.cpp and run ./a.out
    cout << "do stuff" << endl;
    int x = 4;
    cout << x << endl;
    independentMethod(x);
    cout << x << endl;
    Project1 p;
    p.foo(x);
    cout << x << endl;
}

The procedure I go through to test is typical:

mkdir build
cd build
cmake ..
make

I don’t know if this is relevant, but here is my CMakeLists.txt file in the root of my project:

cmake_minimum_required(VERSION 2.8)

# Make PROJECT_SOURCE_DIR, PROJECT_BINARY_DIR, and PROJECT_NAME available
set(PROJECT_NAME MyProject)
project(${PROJECT_NAME})

set(CMAKE_CXX_FLAGS "-g -Wall")

#set(COMMON_INCLUDES ${PROJECT_SOURCE_DIR}/include) if you want your own include/ directory
# then you can do include_directories(${COMMON_INCLUDES}) in other cmakelists.txt files

################################
# Normal files
################################
add_executable(project1 project1.cpp)

################################
# GTest
################################
# This adds another subdirectory, which has project(gtest)
add_subdirectory(lib/gtest-1.6.0)

enable_testing()

# Include the gtest library
# gtest_SOURCE_DIR is available due to project(gtest) above
include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})

################################
# Unit Tests
################################
add_executable(runUnitTests test_project1.cpp)
target_link_libraries(runUnitTests gtest gtest_main)
add_test(NAME runUnitTests COMMAND runUnitTests)

I feel like the issue is due to some misunderstanding I have about implementing .cpp vs in .h. I know that templated functions must be implemented in .h, but I’m not templating anything. I just want to test.

Help appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T19:36:06+00:00Added an answer on June 16, 2026 at 7:36 pm

    You need to link to your runUnitTests executable with your own code in project1.cpp.

    I’d favour making a library out out the guts of project1.cpp and move the main function into a separate file (e.g. main.cpp). Then you can do

    add_library(project1_lib project1.cpp project1.h)
    add_executable(project1 main.cpp)
    target_link_libraries(project1 project1_lib)
    
    add_executable(runUnitTests test_project1.cpp)
    target_link_libraries(runUnitTests project1_lib gtest gtest_main)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.