Problem:
I cannot get the code provided by the author to link via clang++ in chapter 8 of Programming Principles and Practice by Bjarne Stroustrup.
Code:
~/scratch/cpp/chp8 (09/10/2017-13:51:43[EDT]) cat my.cpp
#include <iostream>
#include "my.h"
void print_foo() { std::cout << foo << std::endl; }
void print(int i) { std::cout << i << std::endl; }
~/scratch/cpp/chp8 (09/10/2017-13:52:29[EDT]) cat my.h
#ifndef MY_H
#define MY_H
extern int foo;
void print_foo();
void print(int);
#endif
~/scratch/cpp/chp8 (09/10/2017-13:52:33[EDT]) cat use.cpp
#include "my.h"
int main() {
foo = 7;
print_foo();
print(99);
return 0;
}
Attempts:
I have tried a few different ways of compiling this:
One - Compile the driver app that contains the main function.
~/scratch/cpp/chp8 (09/10/2017-13:52:39[EDT]) clang++ -std=c++14 -stdlib=libc++ use.cpp -o use.cpp.o
Undefined symbols for architecture x86_64:
"print(int)", referenced from:
_main in use-2864c4.o
"print_foo()", referenced from:
_main in use-2864c4.o
"_foo", referenced from:
_main in use-2864c4.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Two - Specifically compile just the my.cpp file and not link. Although I was successful in compiling I'm not sure how to link use.cpp with this object file now.
~/scratch/cpp/chp8 (09/10/2017-14:09:40[EDT]) clang++ -std=c++14 -stdlib=libc++ -c my.cpp -o my.cpp.o
Three - Use all .cpp files.
~/scratch/cpp/chp8 (09/10/2017-14:15:32[EDT]) clang++ -std=c++14 -stdlib=libc++ my.cpp use.cpp
Undefined symbols for architecture x86_64:
"_foo", referenced from:
print_foo() in my-2243d1.o
_main in use-796f91.o
(maybe you meant: __Z9print_foov)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Question:
Besides the obvious question of what am I doing wrong, I'm more interested in what am I missing or miss understanding here?
Note:
I'm not looking for personal opinions or preferences on what I should or should not be doing. This is a purely education adventure that I'm taking with this book and I would like to keep it as such.
Misc:
~/scratch/cpp/chp8 (09/10/2017-14:15:25[EDT]) uname -a
Darwin abes-MacBook-Pro.local 16.7.0 Darwin Kernel Version 16.7.0: Thu Jun 15 17:36:27 PDT 2017; root:xnu-3789.70.16~2/RELEASE_X86_64 x86_64
~/scratch/cpp/chp8 (09/10/2017-14:15:28[EDT]) clang++ -v
Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
The error you get has nothing to do with the difference between compiling and linking. Rather there is an error in your code. The reason for the error is because you are declaring the global variable foo but you never define it. You need to add the line
int foo = 0;
to one of your .cpp files. This probably makes the most sense in my.cpp.
Note that using global variables like this is considered a Coding Horror. You should not do this in real code.
Regarding compiling and linking:
Creating an executable from your C++ takes several steps. Here we will talk about compiling versus linking and how to do each of these steps manually. There are also other steps, but I will not get into them here.
First of all, you can compile and link all of your source files all at once:
$ clang++ -std=c++14 -stdlib=libc++ my.cpp use.cpp
I'm confused why this is not working in your "Attempt three". I will look into this more shortly.
To compile and link your source code manually, you can do the following:
$ clang++ -std=c++14 -stdlib=libc++ -c my.cpp -o my.o
$ clang++ -std=c++14 -stdlib=libc++ -c use.cpp -o use.o
$ clang++ -std=c++14 -stdlib=libc++ my.o use.o -o use
Note that I compile my.cpp to my.o rather than my.cpp.o. This is the common convention for this.
Also, once you are comfortable with these concepts, I strongly suggest that you use a build tool such as make or an XCode project which manages these steps for you automatically.
Related
I've had no problems compiling in the past, and code that I wrote previously is compiling fine, but it is now giving this error when I try to compile a very simple C++ file using Visual Studio Code on my M1 Mac:
Code:
#include<iostream>
using namespace std;
int main() {
cout << "Hello World" << endl;
return 0;
}
The terminal commands I've tried (all give same error):
g++ -std=c++14 test.cpp -o test
g++ -std=c++11 test.cpp -o test
g++ test.cpp -o test
Error:
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I've tried to delete and re-create the file with different names, restarting VSC, using different output file names and nothing changes this.
This was occurring as I hadn't saved the file prior to compiling and therefore the compiler couldn't find it.
First of all I want to say I realise this has been asked a dozen times, and I've read countless solutions without any luck so therefore I'm asking again.
I'm using OS X Mavericks 10.9.5, writing in Sublime 3 and compiling using terminal with g++
I'm trying to compile this simple file (test.cpp)
#include <iostream>
#include <SDL2/SDL.h>
int main () {
if(SDL_Init(SDL_INIT_VIDEO)) {
std::cout << "I made it this far" << std::endl;
}
return 0;
}
Compiler line :
g++ test.cpp
This returns the error :
Undefined symbols for architecture x86_64:
"_SDL_Init", referenced from:
_main in test-ebbae4.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
So, I've tried adding a lot of different flags, -m32 only changes the result to throw Undefined symbols for architecture i386: and ld: symbol(s) not found for architecture i386
I've tried different -arch variations, but I can't seem to get it to work. also played around with -l -I flags but I'm not sure I know what they do/how they could help..
Does anyone have a clue what to do next?
EDIT : Some additional information. I've tried using the .framework for SDL2, that didn't change anything, currently I'm using SDL2 installed through compilation of the source. Headers located in usr/local/SDL2
g++ test.cpp
You should specify the SDL library, too:
g++ test.cpp -lsdl2
You might need to include a path if its not well known to the compiler driver:
g++ test.cpp -L/path/to/the/library -lsdl2
Heres the code, extremely basic Cpp
#include <iostream>
using namespace std;
int main(){
cout << "C++ is FUN!\n";
return 0;
}
The symbols that can not be found are "std" trying to use the name space, and "cout".
the full error message is.
make: *** [FirstProject] Error 1 FirstProject C/C++ Problem
Symbol 'cout' could not be resolved FirstProgram.cpp /FirstProject line 5 Semantic Error
Symbol 'std' could not be resolved FirstProgram.cpp /FirstProject line 2 Semantic Error
symbol(s) not found for architecture x86_64 FirstProject C/C++ Problem
EDIT:
here is the whole linker line:
make all
Building target: FirstProject
Invoking: Cross G++ Linker
g++ -o "FirstProject" ./FirstProgram.o
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [FirstProject] Error 1
Does anyone know what could potentially be the problem?
You are not compiling using a C++ compiler.
If you are using the GNU toolchain then use g++ and not gcc.
You need to compile then link:
g++ -c -o FirstProgram.o FirstProgram.c
g++ -o FirstProject FirstProgram.o
Or you can combine into one statement:
g++ -o FirstProject FirstProgram.c
Here is my make file output:
nvcc -c -arch=sm_35 src/kmeans_cuda.cu
nvcc -c -arch=sm_35 src/sequence.c
nvcc -c -arch=sm_35 src/io.c
nvcc -c -arch=sm_35 src/main.c
nvcc -g -o cuda-means kmeans_cuda.o sequence.o io.o main.o
Undefined symbols for architecture x86_64:
"_kmeans", referenced from:
_main in main.o
(maybe you meant: cudaError (anonymous namespace)::cudaLaunch<char>(char*))
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [all] Error 1
On my main function i call kmeans();, kmeans, is a C function defined in kmeans_cuda.cu
void kmeans() {
long i,h,j,k; //counters
long delta; //Number of objects has diverged in current iteration
long nearest; //Nearest centroid
unsigned int distance,min_distance; //distance calculated by relation point-cluster
int *count,*recv_count;
int *send_label;
double begin,end,trans_init,trans_end;
// should call kernel, but is not calling yet, because it's not implemented ...
I already try to added __host__ on kmeans() declaration, but dind't fixed the problem.
I have now idea why i'm getting this error, because i'm linking the object with the function.
nvcc treats .cu files as C++ and you have your host code in .c files (i.e. in C, not C++).
The easiest solution is to simply rename your .c files to .cpp and treat your app as a C++ app instead of C. You could also declare kmeans() as extern "C" {...} to force the compiler to use a C binding instead of C++ binding for the function, but then you'd have to do that for every future function which is probably unnecessarily inelegant.
So I've spent the last 20 hours trying to get boost working under OS X 10.8, and I have finally gotten it to compile without errors, but when I try to compile a test case that uses Boost.test, I'm back again in a world of hurt.
I should mention, the reason I compile boost myself instead of using the binary available, is because I want to use c++11 and libc++.
When I compiled boost, I called b2 like this:
./b2 toolset=clang cxxflags="-std=c++11 -stdlib=libc++" linkflags="-std=c++11 -stdlib=libc++" link=static
and it compiles all file. Then I have this piece of code
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE Addition
#include <boost/test/unit_test.hpp>
int addition(int i, int j)
{
return i + j;
}
BOOST_AUTO_TEST_CASE(universeInOrder)
{
BOOST_CHECK(addition(2, 2) == 4);
}
which I
try to compile with
clang++ -std=c++11 -stdlib=libc++ -g -Wall -v -I/Users/cb/Downloads/boost_1_51_0 tests/arithmetic.cpp -o tests/arithmetic /Users/cb/Downloads/boost_1_51_0/stage/lib/libboost_unit_test_framework.a`
And it fails miserably, giving me this error:
"/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.8.0 -o tests/arithmetic /var/folders/pg/4wcxn1j12c3188vqrv0x4w9r0000gn/T/arithmetic-UFmO1B.o /Users/cb/Downloads/boost_1_51_0/stage/lib/libboost_unit_test_framework.a -lc++ -lSystem /usr/bin/../lib/clang/4.0/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
"boost::unit_test::unit_test_main(bool (*)(), int, char**)", referenced from:
_main in arithmetic-UFmO1B.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
At first, I figured it was because boost was not compiled for 64bit so I tried telling b2 to do that specifically, but it made no difference, and I also think that it compiles for 64bit by default on OS X.
Any ideas as to why it is failing and how I get it working?
It looks like bad compilation of the test. You requested
#define BOOST_TEST_DYN_LINK
but than you are linking the static version of boost.test
/Users/cb/Downloads/boost_1_51_0/stage/lib/libboost_unit_test_framework.a
and IIRC there is a difference between the static and dynamic versions of this library. So either link the dynamic version of the library (the one with .so extension), or remove that define.