I just installed Boost on my machine. I'm working with the Visual Studio 2010 Ultimate. To install Boost I followed the instruction here: http://www.boost.org/doc/libs/1_48_0/more/getting_started/windows.html. In particular this line: "The installers supplied by BoostPro Computing will download and install pre-compiled binaries into the lib\ subdirectory of the boost root". So I found I have boost_1_47 now running on my machine. And I started a little test program, to play with the boost::thread library. However this code which is the FIRST example code on the introduction to boost::thread won't compile:
#include <boost/thread.hpp>
boost::thread make_thread();
void f()
{
boost::thread some_thread = make_thread();
some_thread.join();
}
int main()
{
f();
return 0;
}
This is the error message:
error LNK2019: unresolved external symbol "class boost::thread __cdecl make_thread(void)" (?make_thread##YA?AVthread#boost##XZ) referenced in function "void __cdecl f(void)" (?f##YAXXZ)
However this code compiles:
#include <boost/thread.hpp>
void testFunction()
{
}
int main()
{
boost::thread_group group;
group.create_thread(&testFunction);
group.join_all();
return 0;
}
The above code I copy/pasted from some forum entry. But what is the reason for all this? Is make_thread() not supported by version 47? If so, why does only the linker complain then? What am I missing?
EDIT:
My apologies for having asked this question, I find it hard to admit, but this belongs to the category RTFM. But however stumbles about this: read the answers below.
After a quick search on Google, and reading the documentation about thread management, it seems to me that the function make_thread is just a dummy function used in an example to show that threads can be moved between different thread objects.
If you want a specific function that creates a thread, you have to make it yourself.
There is no make_thread function defined in boost. I think you misunderstood the example. This line:
boost::thread make_thread();
is just a prototype of a "custom" make_thread function, but without an implementation. That's why the linker fails to find it.
Related
So, I've tried to run the simplest C++ program imaginable in Visual Studio 2022:
main.cpp:
#include "TestClass.h"
int main() {
TestClass().testMethod();
}
TestClass.h:
#pragma once
class TestClass {
public:
void testMethod();
};
TestClass.cpp:
#include "TestClass.h"
inline void TestClass::testMethod() {
}
But for some reason, I get nothing but a linker error:
error LNK2019: unresolved external symbol "public: void __cdecl TestClass::testMethod(void)" (?testMethod#TestClass##QEAAXXZ) referenced in function main
I know that there are tons of questions on Stack Overflow discussing that specific error, but I wasn't able to find anything that applied to my situation, except for this one, which doesn't have an answer.
All files are included in the project (everything was generated in Visual Studio), I do not get any warnings from IntelliSense, and every file on its own compiles just fine (using Ctrl+F7)
I have no clue what is going on and would appreciate any help.
In C++ inline functions must have their body present in every translation unit from which they are called.
Removing inline doesn't change anything
Try to test it. I'm not the only one who proves that inline causes the lnk error. As
Sedenion says, it's a usage error.
I recommend reporting this wrong behavior to Developer Community and posting the link in this thread.
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 4 years ago.
I am trying to create a C++ library that utilizes a third party library. My custom library compiles fine. However, when I try to test it out with a console application, the console application gives me errors from building. My setup is below:
Platform - Visual Studio Community 2017
//MyLib.h
#include <ThirdPartyLib.h>
namespace MyLib
{
class MyLibClass
{
public:
static void SomeFunction();
};
}
//MyLib.cpp
#include MyLib.h
void MyLib::MyLibClass::SomeFunction()
{
ThirdPartyLib::ThirdPartyFunction();
}
//MyConsoleApplication.cpp
#include "..\MyLib\MyLib.h"
#pragma comment(lib,"..\\Debug\\Mylib.lib")
int main()
{
MyLib::SomeFunction();
return 0;
}
My custom library is able to compile fine. When I try to compile the console application, I get a bunch of errors about the third party library like the one below.
LNK2019: unresolved external symbol 'public virtual _thiscall ThirdPartyLib::Foo::~Foo(void)' referenced in function 'private void _thiscall MyLib::MyLibClass::SomeFunction(void)'
I have given my console application the location of where it can find the third party library as well. Can anyone help?
You haven't included ThirdParyLib.lib in you program, have you? What you are getting are linker errors complaining that it cannot find the functions definitions of the functions in ThirdPartyLib.h header file.
Try this :
#pragma comment(lib,"..\\Debug\\ThirdPartyLib.lib")
Please note that ThirdPartyLib.lib is placed in the debug directory as per above example.
I have followed the instructions in the following document to the letter (including the instructions for building mongo-c-driver): https://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/installation/
I am attempting to build the sample code provided (using VS2015), which I shall copy paste for convenience:
#include <iostream>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
int main(int, char**) {
mongocxx::instance inst{};
mongocxx::client conn{ mongocxx::uri{} };
bsoncxx::builder::stream::document document{};
auto collection = conn["testdb"]["testcollection"];
document << "hello" << "world";
collection.insert_one(document.view());
auto cursor = collection.find({});
for (auto&& doc : cursor) {
std::cout << bsoncxx::to_json(doc) << std::endl;
}
}
I have specified the header and library include directories according to the project settings provided. I have also added the MONGOCXX_STATIC and BSONCXX_STATIC preprocessor definitions.
Initially I added the following libs to the linker settings: libmongocxx.lib;libbsoncxx.lib;mongoc-static-1.0.lib;bson-1.0.lib;
When compilation failed I tried using the static bson lib (not sure why the documentation suggests using the non-static one?), so my input libs are now as follows: libmongocxx.lib;libbsoncxx.lib;mongoc-static-1.0.lib;bson-static-1.0.lib;
Despite my best efforts, and frantic googling, I get around 2000 unresolved externals, an example of which I've copied and pasted below:
libmongocxx.lib(distinct.obj) : error LNK2001: unresolved external symbol __imp_mongoc_read_prefs_new
It appears as though it is coming from mongo c driver. I have seen this SO article, but I am already linking mongoc-static-1.0.lib as stated above - unfortunately there is little else to go on in this post. The compiler is clearly finding said lib (otherwise it would complain with "not found" errors), so I can only think that I have somehow built the lib incorrectly.
I'll also mention at this point that I have successfully built and run the code using the non-static libs and dlls - however, it is highly desirable to avoid having to use dlls and so I would like to use the static libs if possible.
I have read the installation instructions several times now, and something that sticks out at me is the following:
If you need static libraries, be sure to use the --enable-static configure option when building libmongoc.
However, this appears to be appropriate only for installing in Linux, I am installing in Windows using CMake. I ran CMake.exe -LH to see what available options there were in the hope of finding something about static libs, but no dice. It appears that there are no options necessary for building the static libs, since when I installed mongo-c-driver, I successfully obtained the bson-static-1.0.lib and mongoc-static-1.0 libs.
I find myself at a loss! Any help would be greatly appreciated, cheers.
Have you resolved the problems? I have similar problems. However, it seems fine if you use older drivers, like mongoc 1.4, bson 1.4 and mongocxx 3.0, though they can only support mongodb 3.2 and older
I had a solution named fun.sln with a project called fun.vcxproj.
I created a whole bunch of name spaces ready to be used.
I made another project called no_more_fun.vcxproj.
I added the includes directory for fun.vcxproj to the configuration of no_more_fun.vcxproj.
I added this to no_more_fun.cpp
#include "candy.h"
void main(void)
{
candy::get();
return;
}
candy.h is in the default directory for fun.vcxproj(which was added to the config)
But I get...
LNK2001 unresolved external symbol "int __cdecl candy::get(unsigned long)" (?get#candy##YAHK#Z) .....
Visual Studio shows no error before compiling.
The "candy" namespace works fine in the "fun" project so idn...
Is there a guide or something so that i can understand how i can go about sharing code efficiently among different projects within ONE solution?
This is a linker error. You didn't get any error at compile time, because the compiler found the candy::get() method in candy.h header, but the implementation (which I suppose is in a candy.cpp file) is not found by the linker.
Just add candy.cpp too to no_more_fun.vcxproj.
ps. I didn't observe but in the error message you can see that the function waits a parameter.
call it like this:
unsigned long foo = 0;
candy::get(foo);
This is going to sounds stupid but...i just dragged the files in to visual studio so that the no_more_fun project had the "files" in its "directory" too.
wow... I shouldn't need to do that...Am I wrong?(sarcasm).
I am currently trying to learn how to use premake 4 in order to apply it to the OpenGL sdk. I am currently trying to make a Visual Studio 2010 solution that constructs 2 projects, one being a static library, the other contains a single main source file, with the main method.
This project is extremely simple, and is solely for the purpose of learning premake. In the static library project, named Test, I have 2 files, Test.h and Test.cpp. Test.h contains the prototype for the method print(). print() simply prints a line to the console. Using premake, I linked the static library to the Main project, and in main.cpp I have included the Test.h file. My problem is this: in VS2010 I get this error when I attempt to build:
1>main.obj : error LNK2019: unresolved external symbol "void __cdecl print(void)" (? print##YAXXZ) referenced in function _main
1>.\Main.exe : fatal error LNK1120: 1 unresolved externals
Here is my code in the 4 files, the premake4.lua:
solution "HelloWorld"
configurations {"Debug", "Release"}
project "Main"
kind "ConsoleApp"
language "C++"
files{
"main.cpp"
}
configuration "Debug"
defines { "DEBUG" }
flags { "Symbols" }
configuration "Release"
defines { "NDEBUG" }
flags { "Optimize" }
links {"Test"}
project "Test"
kind "StaticLib"
language "C++"
files{
"test.h",
"test.cpp"
}
Test.cpp:
#include <iostream>
void print(){
std::cout << "HELLO" << std::endl;
}
Test.h:
void print();
Main.cpp:
#include <conio.h>
#include "test.h"
int main(){
print();
getch();
return 0;
}
If you are wondering why there is a getch() there, on my computer the console immediately closes once it reaches return 0, so I use getch() to combat that issue, which forces the window to wait until the user has pressed another key. Any advice on this issue would be wonderful, because I simply am not sure what the problem is. If it is something simple please dont castrate me on it, I have very little experience with premake and static libraries, which is why I am trying to learn them.
links {"Test"}
Lua is not Python. Whitespace is irrelevant to Lua, just like whitespace doesn't matter to C++. So your links statement only applies to the "Release" configuration. If you want it to apply to the project as a whole, it needs to go before the configuration statement, just like your kind, files, and other commands.
Premake4 works this way so that you could have certain libraries that are only used in a "Release" build (or Debug or whatever). Indeed, you can put almost any project command under a configuration. So you can have specific files that are used only in a debug build, or whatever.