Cannot specify additional link file for clang - c++

I've successfully compiled my main.cpp with clang, specifying additional include path via the command line options as follows: clang++ -I ./Dependencies/GLFW/include/ -S .\main.cpp.
However, when I try to link it by specifying the additional link library by the following command: clang++ -L ./Dependencies/GLFW/lib/glfw3.lib .\main.s it gives me a linker error main-8b7c4e.o : error LNK2019: unresolved external symbol glfwInit referenced in function main.
Any suggestions on what might be wrong? I'm sure that the relative path specified is correct, since the compile command gave me no issues.
main.cpp
#include <iostream>
#include <GLFW/glfw3.h>
int main() {
glfwInit();
std::cout << "Hello world" << std::endl;
return 0;
}

-L is for specifying folders to search for libraries in. The compiler ignores directories that don't exist.
Your command line should be:
clang++ ./Dependencies/GLFW/lib/glfw3.lib .\main.s

Please find below link for more detail about glfw header inclusion.
https://www.glfw.org/docs/latest/build_guide.html

Related

new c++ error with file

I am 100% new at c++ so bear with me :)
I am getting an error with this file and not sure why. any help is appreciated.
#include <iostream>
using namespace std;
int main()
{
cout << "hi" << endl;
return 0;
}
------------ Build: Debug in 1600 (compiler: GNU GCC Compiler)-------------
g++ -o bin/Debug/1600 obj/Debug/main.o obj/Debug/src/test.o obj/Debug/test03.o
duplicate symbol _main in:
obj/Debug/main.o
obj/Debug/test03.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
From the source files symbols are created. main in the .cpp file becomes _main as a symbol. During linking there can be only one main function, hence only one _main symbol is expected and allowed.
You have three object files that were created:
obj/Debug/main.o which contains main
obj/Debug/src/test.o
obj/Debug/test03.o which also contains main
Probably because you have a .cpp file for each of them and the command line or IDE you are using asked for them all to be compiled.
duplicate symbol _main
The text above is telling you that the linker (trying to make sense of all the compiled object (.o) files) found more than one main.
So the solution is to look at your IDE settings and remove the other files (or at least remove main from the other files) because you are only interested in compiling the one source file.
Its hard to tell what you're running from the question.
Here is how to build a simple C++ program using gcc
In
my_program.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "hi" << endl;
return 0;
}
To compile to object files type
g++ -c my_program.cpp
To link (you'd normally have more files here)
g++ -o my_program my_program.o
So, this isn't very fun so most people use a build system like make, cmake, msbuild or whatever the CLion IDE uses.

Linking static C++ archive file in Hello World app

I'm trying to link an archive file into my simple hello world app, to make sure I understand the process. However, I obviously don't, because the library isn't linking correctly.
Here is my simple app (hello.cc):
#include <iostream>
#include "firebase/app.h"
int main()
{
std::cout << "Hello, world!\n";
std::cout << std::endl;
return 0;
}
And here is my compilation command with linking:
gcc hello.cc -L /tmp -l app -o hello -lstdc++
I've moved my archive file (libapp.a) to /tmp.
My understanding is that -L <dir> adds directories to search for lib*.a files and -l <name> indicates the name of the archive files, in the form of lib<name>.a. I know it isn't linking the archive file, because I get this error:
hello.cc:3:26: fatal error: firebase/app.h: No such file or directory
#include "firebase/app.h"
However, I also don't think the compilation command does what I think it does, because I renamed libapp.a to libapp.a2 and the same error was returned, not something indicating that the archive file was missing.
Can somebody help me with a) the command to link the library file /tmp/libapp.a and b) explain what I'm doing wrong?
EDIT
I forgot to include the headers, which caused the initial error I think. Now that I'm including the headers, with the following:
gcc hello.cc -L /tmp -l app -I /tmp/firebase/include -o hello -lstdc++
I get this new error:
/usr/bin/ld: cannot find -lapp
collect2: error: ld returned 1 exit status
So, I think I'm not using -L and -l correctly, but not sure what exactly I'm doing wrong.
EDIT2
Fixed. Forgot I had renamed the lib file while trying to figure out the first issue. Once I named it back to libapp.a the app worked as expected.

undefined reference to glfwSetErrorCallback

I'm trying to use the GLFW library, but am having difficulty with compiling a simple program. I went to the GLFW website and download the latest release, then using "How to build & install GLFW 3 and use it in a Linux project" I built and installed it.
Here's my code:
#include <GLFW/glfw3.h>
#include <iostream>
using std::cout;
using std::endl;
void GLFW_error(int error, const char* description)
{
fputs(description, stderr);
}
void run()
{
cout << "pooch" << endl;
}
int main()
{
glfwSetErrorCallback(GLFW_error);
if (!glfwInit()) exit(EXIT_FAILURE);
run();
glfwTerminate();
return 0;
}
Using the command line:
bulletbill22#ROBOTRON ~/Desktop $ g++ -std=c++11 -lglfw source.cpp
yields
source.cpp:function main: error: undefined reference to 'glfwSetErrorCallback'
glfwSetErrorCallback is taken from their tutorial for "Setting an error callback".
Inclusion of -glfw3 results in /usr/bin/ld: error: cannot find -lglfw3
Even though everything seemed to be installed correctly, I suspect the problem may lie somewhere with the installation of the GLFW library because I'm not used to CMake and don't entirely understand how it works. I'm frustrated because the answer must be simple, but I'm not sure which keywords are really relevant when googling the problem; mostly the results are people who were incorrectly compiling with CMake, which I'm not compiling with in this case.
It seems that the directories for the glfw3.h header and libglfw3.so (and/or libglfw3.a) library are not in the default path.
You can check with by adding the -v option to the g++ options. Locate the directory where the glfw3.h header is found - call this $GLFW_INCDIR - it typically ends with .../GLFW. Locate the directory where the library is found - call this $GLFW_LIBDIR. Try:
g++ -std=c++11 -I$GLFW_INCDIR source.cpp -o pooch -L$GLFW_LIBDIR -lglfw3
If all the library dependencies are satisfied, this hopefully results in a program called pooch.
One other thing: GLFW3 is a C library, and the callback function arguments are expected to be C functions. So your callback should have 'C' linkage, i.e.,
extern "C" void GLFW_error(int error, const char* description) ...
Also, if you're having trouble with cmake, you may have ccmake installed. Try ccmake . in the top-level directory of the GLFW3 package for 'interactive' configuration.

/usr/bin/ld: cannot find -llibboost

Alright So right now I am attempting use the boost C++ libraries in Linux (Ubuntu 12.04) as I have previously used them in Windows. So using some example code from the Boost's site
testfile.cpp
#include <boost/filesystem/convenience.hpp>
#include <boost/foreach.hpp>
#include <boost/range.hpp>
#include <iostream>
int main(int, char**)
{
namespace bf = boost::filesystem;
BOOST_FOREACH(bf::path path,
boost::make_iterator_range(
bf::recursive_directory_iterator(bf::path("/home")),
bf::recursive_directory_iterator())) {
std::cout << path.string() << std::endl;
}
return 0;
}
Should very easily compile using this command
g++ -L/usr/local/lib -o "testfile" -llibboost_filesystem
My problem I am getting the linker error
/usr/bin/ld: cannot find -llibboost_filesystem
and cannot seem to figure out what I am missing. Please Help.
By convention, library names use the lib prefix on most Linux distributions. You should remove this prefix when instructing the linker which libraries to search for. Assuming the gnu ld linker, the documentation says
-l namespec
--library=namespec
Add the archive or object file specified by namespec to the list of files to
link. This option may be used any number of times. If namespec is of the
form :filename, ld will search the library path for a file called filename,
otherwise it will search the library path for a file called libnamespec.a.
so you either want
g++ -L/usr/local/lib -o "testfile" -lboost_filesystem
or
g++ -L/usr/local/lib -o "testfile" -l :libboost_filesystem.so

xerces-c 2.8 : error while loading shared libraries

I'm trying to compile a program running on an HP UX server on a Red Hat Linux.
It uses xerces-c library to parse xml files. Compilation is ok, but when i try to run it, I get the following message
./a.out: error while loading shared
libraries: libxerces-c.so.28: cannot
open shared object file: No such file
or directory
I wrote a very simple program to try and understand whats going on:
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/util/TransService.hpp>
#include <xercesc/parsers/SAXParser.hpp>
#include <xercesc/util/OutOfMemoryException.hpp>
int main(int argc, char* argv[])
{
return 0;
}
And compiled it like this:
g++ test.cpp
-L./xml/xerces-c_2_8_0/lib -lxerces-c -I./xml/xerces-c_2_8_0/include
Surprisingly the file is actually there:
lib]$ ls
libxerces-c.a libxerces-c.so.28 libxerces-depdom.a libxerces-depdom.so.28
libxerces-c.so libxerces-c.so.28.0 libxerces-depdom.so libxerces-depdom.so.28.0
Any thoughts ? I feel i'm missing something, but don't know what.
Thanks in advance.
run ldd a.out and see if the linker can resolve the right .so file
export LD_LIBRARY_PATH to include the current folder (in the same manner as the PATH variable) and check ldd again
the good way to do what you want is the following one:
g++ test.cpp -Xlinker -R ./xml/xerces-c_2_8_0/lib -lxerces-c -I./xml/xerces-c_2_8_0/include
or
g++ test.cpp -Wl,-rpath ./xml/xerces-c_2_8_0/lib -lxerces-c -I./xml/xerces-c_2_8_0/include
Xlinker or Wl options allow you to use specific linking options, you do not need to modifiy
LD_LIBRARY_PATH
You need to tell the runtime c library where to find the various symbols that arent compiled statically in your code and arent in the usualy /lib and /usr/lib locations.
You do this by adding the path to your shared library to LD_LIBRARY_PATH. In this case, this will be what you have been putting for the -L argument to the compiler.