I have set up two solutions to illustrate my problem, a total of three projects. I compile in Debug mode but my Problem still exists even if I compile in Release mode.
MyRunnerCore.lib requires 3rdParty.lib. Why is that, what can I do against it?
Here is a Picture to explain how the folders are structured:
The idea is that I build the third party as a lib Project (works fine). Then I build the MyRunnerCore using the lib file in a cpp file only (works fine). Last not Least I Build a Console Application that uses the MyRunnerCore.lib (LNK1104). The output window reads:
1>------ Build started: Project: MyRunnerCore, Configuration: Release Win32 ------
1> Core.cpp
1> MyRunnerCore.vcxproj -> C:\SO\MyRunner\Release\MyRunnerCore.lib
2>------ Build started: Project: MyRunner, Configuration: Release Win32 ------
2> main.cpp
2>LINK : fatal error LNK1104: cannot open file '3rdParty.lib'
========== Build: 1 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Here are the sourcefiles with annotations to point out in what Project I edited some settings.
ThirdPartyClass.cpp
#include "ThirdParyClass.hpp"
int ThirdParyClass::GenerateNumber()
{ return 4; }
ThirdPartyClass.hpp
#pragma once
class ThirdParyClass
{ public: int GenerateNumber(); };
main.cpp
//MyRunner Properties:
//Project Dependencies Added MyRunnerCore
//Include Directories Added $(SolutionDir)
//Library Directories Added $(OutDir)
#include <MyRunnerCore\Core.h>
#pragma comment (lib, "MyRunnerCore.lib")
int main() { Core c{}; return c.Run(); }
Core.cpp
#include "Core.h"
//MyRunnerCore Properties:
//Added To Include Path C:\SO\3rdParty
//Added To Library Path C:\SO\3rdParty\Debug
#include <3rdParty\ThirdParyClass.hpp>
#pragma comment(lib, "3rdParty.lib")
int Core::Run()
{
ThirdParyClass tp{};
return tp.GenerateNumber();
}
Core.h
#pragma once
class Core
{ public: int Run(); };
Why does the Linker require 3rdParty.lib to link?
Am I missing a setting to make the Linker build MyRunnerCore.lib build without references to 3rdParty.lib?
It seems that the linker doesn't know where to find the file ThirdParyClass.lib. In the project settings for MyRunner, add the folder containing this file to Additional Library Directories under Linker.
As tsandy wrote:
Librarian -> General -> Link Library Dependencies -> Yes
is correct. However using the following
#pragma comment (lib, ...)
is incompatible with that.
The library has to be included using
Librarian -> Additional Libraries -> 3rdParty.lib;%(AdditionalDependencies)
Thanks to tsandy for the Input.
Related
In a nutshell
I am tring to use an existing C++ library.
The problem is that it's not compiled.
I took the src code (a visual studio project) and tried to compile it.
The thing is that I can't link my project to the library.
More specific details are:
What I have already done is the following:
I opened the .sln file and added another sub-project called "Sampler"
I downloaded all Microsoft additional packages that the library src code depends on.
I compiled the library to Windows 32 platform x86 (using Visual Studio 2022). The compilation was successful.
I created a directory Dependencies\OPCClientToolKit in same directory of .sln file, Dependencies\OPCClientToolKit contains two directories include and lib.
I put in Dependencies\OPCClientToolKit\lib the .lib - the output after compilation.
I put in Dependencies\OPCClientToolKit\include the .h files of the src code of sdk library.
I added a dependency .h files via Sampler Project > Properies > C++ General. See pictures below.
I added a dependency folder Dependencies\OPCClientToolKit\lib via Sampler Project > Properies > Linker General. See pictures below.
I added a dependency OPCClientToolKit.lib via Sampler Project > Properies > Linker Input. See pictures below.
As far as I know all my step seems valid and fine.
But when, I am compiling (hitting Build via Visual Studio) the Sampler project code (see the code below) I am getting an error message:
Error Message:
1>------ Build started: Project: Sampler, Configuration: Release Win32 ------
1>Sampler.cpp
1>Sampler.obj : error LNK2001: unresolved external symbol "public: static class COPCHost * __cdecl COPCClient::makeHost(class ATL::CStringT<wchar_t,class ATL::StrTraitATL<wchar_t,class ATL::ChTraitsCRT<wchar_t> > > const &)" (?makeHost#COPCClient##SAPAVCOPCHost##ABV?$CStringT#_WV?$StrTraitATL#_WV?$ChTraitsCRT#_W#ATL###ATL###ATL###Z)
1>C:\Users\Mark\Desktop\Projects\PID\OPC_DA\Release\Sampler.exe : fatal error LNK1120: 1 unresolved externals
1>Done building project "Sampler.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Please explain to me how should I resolve this issue. I just want to include and use this library and not to modify it.
Dependecies\OPCClientToolKit\lib
Dependecies\OPCClientToolKit\include
Sampler Project > Properties > C++ > General
Sampler Project > Properties > Linker > General
Sampler Project > Properties > Linker > Input
I am using this library (OPC client sdk):
https://sourceforge.net/projects/opcclient/
A usage example made by other guy on the internet:
https://github.com/Tibalt/OPC_DA/blob/master/OPCClientDemo/OPCClientDemo.cpp
My code just use functions and objects of that library.
In details, just trying to initialize opc connection to a server on localhost:
#include <iostream>
#include <vector>
#include <string>
#include <stdio.h>
#include <sys\timeb.h>
#include "opcda.h"
#include "OPCClient.h"
#include "OPCHost.h"
#include "OPCServer.h"
#include "OPCGroup.h"
#include "OPCItem.h"
using namespace std;
int main(void) {
COPCClient::init();
cout << "Done Init";
cout << endl;
CString hostName = "localhost";
COPCHost* host = COPCClient::makeHost(hostName);
}
That project OPCClientToolKit is very old. It looks like built with using ANSI character set, without using the macro _UNICODE. You should either
Remove the macro _UNICODE from the preprocessor settings in your project.
Or add that entire project OPCClientToolKit as a dependency to your visual studio solution and add the macro _UNICODE to that project.
Or use the solution of that project OPCClientToolKit and rebuild the static library OPCClientToolKit.lib with the macro _UNICODE.
I have 2 small projects in a vs solution: app and lib.
app has a dependency on lib. This is specified via "Reference".
lib is a static library.
Here is lib source:
a.h:
#pragma once
namespace lib{
void a();
#ifdef UNIT
void aa();
#endif
}
a.cpp:
#include <a.h>
void lib::a() { }
#ifdef UNIT
void lib::aa() { }
#endif
Here is app source:
#include <a.h>
void f()
{
lib::a();
#ifdef UNIT
lib::bb();
#endif
}
int main()
{
f();
}
Here is how solution explorer looks:
Being a develeoper I would like to specify preprocessor defines in app and trigger lib recompilation with these defines. In this particular case UNIT.
I have a very simple need for that.
When used in unit tests, lib should provide different implementation.
When I fully rebuild app, linker outputs a linkage error:
1>------ Rebuild All started: Project: lib, Configuration: Release Win32 ------
1>a.cpp
1>lib.vcxproj -> C:\test\static-lib\Release\lib.lib
2>------ Rebuild All started: Project: app, Configuration: Release Win32 ------
2>app.cpp
2>C:\test\static-lib\app\app.cpp(14,10): error C2039: 'bb': is not a member of 'lib'
2>C:\test\static-lib\lib\include\a.h(2): message : see declaration of 'lib'
2>C:\test\static-lib\app\app.cpp(14,12): error C3861: 'bb': identifier not found
2>Done building project "app.vcxproj" -- FAILED.
Build has been canceled.
Build time 00:00:04.082
Build ended at 4/8/2020 4:04:34 PM
From which I conclude that lib is recompiled WITHOUT UNIT.
This is a very basic thing of a build system and somehow I cannot get it to work.
Is there something I'm missing? I would be grateful for comments.
vs2015 community, x64, debug, boost 1.63
New Empty project
Properties->C++->General->Additional Include Directories add
"C:\Program Files\boost_1_63_0" Add new C++ file, Source.cpp:
#include "boost/make_shared.hpp"
#include "boost/thread.hpp"
void main(int argc, char **argv)
{
}
Build Solution
Result:
1>------ Build started: Project: boostLibTest, Configuration: Debug x64 ------
1> Source.cpp
1>LINK : fatal error LNK1104: cannot open file 'libboost_thread-vc140-mt-gd-1_63.lib'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Where is that lib file getting added to the project? It's not boostLibTest.vcxproj, nor the command line for the compiler.
I wanted to write a static library that uses boost that I can access from another app that doesn't have/need boost, but this auto-include-boost-dependency prevents me from doing so.
There are #pragmas that MSVC supports that let a header file state "you need this library".
Boost is apparently using them.
Ideally, boost should only include them in header files that are not "header only". The granularity may not be perfect. But if you only need some enum values and other header-file only data from "boost/thread.hpp", check to see if they are included in a "header-file-only" header.
I'm getting this linker error when I try to compile the hello world .cpp after installing the FLTK kit.If you notice the library names are from installation found in appendix D in the stroustrup book "programming principles and practice". The edition is 2012 but it seems that FLTK version is a bit different now, for instance the version the book recommends to down load is FLTK 1.1.(?), and 1.3.2 is the latest. I think the linker problem is inside my VC++ project under the project/properties/linker/input/additional dependencies tab I put (per appendix d)
fltk.lib
wsock32.lib
comctl32.lib
fltkjpegd.lib
fltkimagesd.lib
But the .lib files I copied from the fltk lib folder didn't have those names. They are named:
fltkzlibd
fltkpngd
fltkjpegd
fltkimagesd
fltkformsd
fltkgld
fltkd
Are these the .lib files to include in VC++ project under the project/properties/linker/input/additional dependencies tab ? If not, how else can I fix this mess?
1>------ Build started: Project: test, Configuration: Debug Win32 ------
1> test.cpp
1>LINK : fatal error LNK1104: cannot open file 'fltk.lib wsock32.lib comctl32.lib fltkjpegd.lib fltkimagesd.lib'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Box.H>
int main(int argc, char **argv)
{
Fl_Window *window = new Fl_Window(300,180);
Fl_Box *box = new Fl_Box(20,40,260,100,"Hello, World!");
box->box(FL_UP_BOX);
box->labelsize(36);
box->labelfont(FL_BOLD+FL_ITALIC);
box->labeltype(FL_SHADOW_LABEL);
window->end();
window->show(argc, argv);
return Fl::run();
}
Include those lib files for the debug version. The release versions don't have the trailing d.
For the linking, use ws2_32.lib instead of wsock32.lib.
When adding your additional dependencies, did you click on the ellipsis and then in the dialog enter all the library names on one line? They need to be entered on separate lines.
I've copied the FL folder into the project.
and it show me this:
1>------ Build started: Project: Client, Configuration: Debug Win32
------ 1> Main.cpp 1>c:\users\user\documents\visual studio 2012\projects\talktome\talktome\fl\xutf8.h(33): fatal error C1083:
Cannot open include file: 'X11/X.h': No such file or directory
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
my source code is
using namespace std;
#include "FL\Fl.H"
#include "FL\Fl_Window.H"
#define WIDTH 700
#define HEIGHT 500
int main()
{
Fl_Window win(WIDTH, HEIGHT, "TalkToMe");
win.begin();
win.end();
win.show();
return Fl::run();
}
You should check if there is "#define WIN32" before your FLTK includes. will help you. It should. Simular problem here:
FLTK in MSVC needs x11 headers?
Do not use the \ in the include statements. Use the forward slash / .
The problems you refer to in your comment to Mycotoxin clearly indicate you have linking problems. You have to tell your compiler where to find the fltk library and the header files. Unresolved external symbols mean only one thing you know... :)
You do not have to define WIN32 as described in Mycotoxin's text. The compiler does that for you, and FLTK uses this fact. Even if it does not, you typically give it as a parameter to the compiler (something like -DWIN32 in the case of GCC or similar for CL).
Watch Greg's video tutorial at http://seriss.com/people/erco/fltk-videos/ where he explains how to configure FLTK and build a small app using Microsoft Visual Studio 7.
Finally, get the source package, and read the README.MSWindows.txt file. It explains everything you need to know in order to build your FLTK-based application on Windows.