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

  • SEARCH
  • Home
  • 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 9259719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:43:20+00:00 2026-06-18T12:43:20+00:00

Edit: I have updated my question with changes I’ve made, upon answers. I’m trying

  • 0

Edit: I have updated my question with changes I’ve made, upon answers.
I’m trying to link to a little library that I’ve wrote to learn ho this is done with C++ with no luck. G++ is complaining with undefined reference.

The root directory of library I want to link is in directory ~/code/gklib/cxx/. The structure of this directory is as follows:

~/code/gklib/cxx/
|
|`-> gk.{hh,cc}
|`-> libgk.o
|
 `-> lib/
     |
     `-> libgk.a

I have compiled gk.cc with -c flag, then transformed the resulting object file to a .a file with ar rvsc lib/libgk.a libgk.o.

The client to this library is at folder ~/code/cpp. In this directory I compiled some_stuff.cc to an object file again, then I tried to link to it with this command:

$ cxx some_stuff.o -L../gklib/cxx/lib -lgk -o some_stuff

I get this error:

some_stuff.o: In function `main':
some_stuff.cc:(.text+0x49): undefined reference to `void GK::algorithms::insertionSort<int, 5ul>(int*)'
collect2: error: ld returned 1 exit status

These are contents of these files:

~/code/cpp/some_stuff.cc

#include <cstdlib>
#include <iostream>
#include <gk.hh>

using namespace std;

int main(int argc, char **argv) {
  int i = -1;
  int arr[5] = { 3, 4, 2, 1, 5 };
  const size_t len = sizeof(arr)/sizeof(int);
  GK::algorithms::insertionSort<int, len>(arr);
  while(++i < 5)
    cout << arr[i] << " ";
  cout << endl;
  exit(EXIT_SUCCESS);
}

~/code/gklib/cxx/gk.cc

#include "gk.hh"
template<class T, size_t len>
void GK::algorithms::insertionSort(T arr[len]){
  // insertion sort
}

~/code/gklib/cxx/gk.hh

#pragma once
#include <cstdlib>

#define NAMESPACE(ns) namespace ns {
#define END_NAMESPACE(ns) }

NAMESPACE(GK)
NAMESPACE(algorithms)

template<class T, size_t len>
extern void insertionSort(T arr[len]);

END_NAMESPACE(algorithms)
END_NAMESPACE(GK)

I’ve tried many variations on my commands with no result. Internet is full of tutorials and forums with instructions those did not work for me. This code ran perfectly when all the stuff was in one file. How can I resolve this problem? Thanks in advance.

  • 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-18T12:43:21+00:00Added an answer on June 18, 2026 at 12:43 pm

    I see several problems: in order:

    • If this is your exact command line, the -L option doesn’t
      point to the structure you’ve shown above (where there is no
      cxx in the path).

    • I don’t know why you’re using -B. This tells the compiler
      where to look for its libraries, etc. Normally, this is only
      necessary when you want to test parts of the compiler you’ve
      modified.

    • I don’t see where you’ve specified to link against the
      library. Since the library doesn’t respect the usual naming
      conventions (libname.a), you’ll have to
      specify it directly (in the same way you’d specify an object
      file), and the -L option isn’t used. Alternatively, you name
      it libgk.a, or something like that, and add a -lgk to the
      command line, after your object files.

    • Finally, the error messages refer to an instantiation of
      a template. This typically occurs because the implementation of
      the template is in a source file, not in the header. The
      standard requires that the implementation be visible when it
      triggers the instantiation of a template. The way g++ (and
      almost every other compiler) works is that if the implementation
      isn’t visible, they suppose that the template will be
      instantiated in another translation unit, and output external
      references to it. But if the implementation is in a source,
      there will be no instantiation, and you’ll get a linker error.
      (You’ll also not find the assembler for the instantiation
      anywhere, either, because the template hasn’t been
      instantiated.) You should include the source code at the bottom
      of your header, and not compile it separately.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit (updated question) I have a simple C program: // it is not important
EDIT: I have no updated the question, whilst doing so I realized the scope
EDIT: I have changed the question as it was too localized and gained negative
I'm trying to ensure that visitors of my ASP.NET MVC website always have the
I have a main task (to develop a library) that I split up to
(EDIT: Question changed.) I have a product with an installer which was built by
I have a table with various fields that can be updated. Let's called it
This is a follow up to a question that was asked yesterday. I have
EDIT: I found that the issue is actually that IE changes an HTML elements
EDIT I have finally figured out the problem i have been having on my

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.