I Am on a Mac and I tried to compile OpenGL and GLUT but it does not work, why ?
Here is the command line that try to build (I use netbeans but this is the command line output window):
g++ -o dist/Debug/GNU-MacOSX/cppapplication_1 build/Debug/GNU-MacOSX/main.o -L/System/Library/Frameworks/OpenGL.framework/Libraries -lGL -lGLU
Undefined symbols for architecture x86_64:
"_glutInitWindowPosition", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Here is my code:
#include <cstdlib>
#include <iostream>
#include <glut.h>
using namespace std;
/*
*
*/
int main(int argc, char** argv) {
glutInitWindowPosition(1,1);
return 0;
}
Here is my netbeans config
For information, there is no lib directory in /System/Library/Framework/GLUT.framework...
Try with these -framework GLUT -framework OpenGL
and possibly:
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <OpenGL/glext.h>
#include <GLUT/glut.h>
Related
I am trying dynamic-linking test.
mylib.cpp
#include<iostream>
#include<stdio.h>
using namespace std;
int hello(){
cout<<"Hello,World!"<<endl;
return 1;
}
then compile
g++ -shared -o libmylib.so mylib.cpp
Now there appears the libmylib.so
test.cpp
#include <iostream>
int hello();
int main() {
hello();
std::cout << "Test Finish!\n";
return 0;
}
Try to compile with this,
g++ -o test test.cpp -L ./
There comes the error
Undefined symbols for architecture x86_64:
"hello()", referenced from:
_main in test-37bd2a.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Do I need to add some options or are there something wrong in my source code??
Thank you for your help.
Do I need to add some options
yes, link with the library.
g++ ... -lmylib
I'm trying to do a test of basic linking for cpp files, I've been searching all over and am having a lot of trouble trying to find a solution. I understand that I have to include the header in both cpp's, but I'm having trouble trying to run these two together.
//testMain.cpp
#include <iostream>
#include <stdio.h>
#include "func.h"
using namespace Temp;
int main()
{
getInfo();
return 0;
}
//func.h
#ifndef FUNC_H
#define FUNC_H
#include <iostream>
#include <stdio.h>
namespace Temp{
int getInfo();
}
#endif
//functions.cpp
#include "func.h"
using namespace std;
int Temp::getInfo()
{
return 5 + 6;
}
//error that I'm getting using VS Code
cd "/Users/jcbwlsn/Downloads/Coding/CPP/Workspace/RPG Project/src/" && g++ testMain.cpp -o testMain && "/Users/jcbwlsn/Downloads/Coding/CPP/Workspace/RPG Project/src/"testMain
Undefined symbols for architecture x86_64:
"Temp::getInfo()", referenced from:
_main in testMain-1f71a1.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
You're supposed to specify all translation unit files when linking a C++ program.
Your program consists of two source files, testMain.cpp and functions.cpp.
Hence the compile-and-link command should be something like:
g++ testMain.cpp functions.cpp -o testMain
Alternatively you can compile each source into separately and then link them into an executable:
g++ -c testMain.cpp -o testMain.o
g++ -c functions.cpp -o functions.o
g++ testMain.o functions.o -o testMain
Having some kind of a Makefile helps to automate this.
I'm new in cpp and when I compile two cpp files in terminal and shows the error as followed
Undefined symbols for architecture x86_64:
"func(int)", referenced from:
_main in main-45889e.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
here are my codes:
main.cpp
#include <stdio.h>
#include <iostream>
#include "func.hpp"
int main(int argc, char* args[]){
func(1);
return 0;
}
func.cpp
#include "func.hpp"
#include <iostream>
int func(int a){
std::cout<<a<<std::endl;
}
func.hpp
int func(int a);
it succeed when I use Xcode to run the code while failed when using terminal to compile with the following g++ commands:
g++ -c main.cpp
g++ -c func.cpp
g++ main.o func.o -o main.out
Please help
I am trying to run the basic "Hello, World!" example:
#include <boost/mpi/environment.hpp>
#include <boost/mpi/communicator.hpp>
#include <iostream>
namespace mpi = boost::mpi;
int main()
{
mpi::environment env;
mpi::communicator world;
std::cout << "I am process " << world.rank() << " of " << world.size()
<< "." << std::endl;
return 0;
}
I have tried numerous variants for running this program:
mpic++ -I /usr/local/include/ test.cpp -o test -lboost_system
also:
mpic++ -I /usr/local/include/boost test.cpp -o test -lboost_system
and using mpicc, and clang++ as a substitute. Each combination gives the follow error:
Undefined symbols for architecture x86_64:
"boost::mpi::environment::environment(bool)", referenced from:
_main in test-b0215f.o
"boost::mpi::environment::~environment()", referenced from:
_main in test-b0215f.o
"boost::mpi::communicator::communicator()", referenced from:
_main in test-b0215f.o
"boost::mpi::communicator::rank() const", referenced from:
_main in test-b0215f.o
"boost::mpi::communicator::size() const", referenced from:
_main in test-b0215f.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Homebrew says that both MPICH2 and boost1.63.0 are installed. I can confirm that mpic++ runs by compiling and then running this program:
// required MPI include file
#include "mpi.h"
#include <stdio.h>
int main(int argc, char *argv[]) {
int numtasks, rank, len, rc;
char hostname[MPI_MAX_PROCESSOR_NAME];
// initialize MPI
MPI_Init(&argc,&argv);
// get number of tasks
MPI_Comm_size(MPI_COMM_WORLD,&numtasks);
// get my rank
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
// this one is obvious
MPI_Get_processor_name(hostname, &len);
printf ("Number of tasks= %d My rank= %d Running on %s\n", numtasks,rank,hostname);
// do some work with message passing
// done with MPI
MPI_Finalize();
}
Which produces the correct output.
I have also verified that (at least part of) Boost is installed by compiling and successfully running the Boost "Hello, World!"
//
// timer.cpp
// ~~~~~~~~~
//
// Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
int main()
{
boost::asio::io_service io;
boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
t.wait();
std::cout << "Hello, world!" << std::endl;
return 0;
}
with:
clang++ -I /usr/local/include/ timer.cpp -o timer -lboost_system
How can I get the Boost.MPI example to run?
When you get linker errors from boost, you usually forgot to link against a boost library.
There is also a boost_mpi library, so you should compile with
clang++ -I /usr/local/include/ timer.cpp -o timer -lboost_system -lboost_mpi
Note that you need a boost version with support for mpi.
Generally consider http://www.boost.org/doc/libs/1_58_0/doc/html/mpi/getting_started.html
Specifically with homebrew you can check How to build boost with mpi support on homebrew?
main.cpp
#include <iostream>
#include "Burrito.h"
using namespace std;
int main(){
Burrito b;
return 0;
}
Burrito.h
#ifndef BURRITO_H
#define BURRITO_H
class Burrito{
public:
Burrito();
};
#endif
Burrito.cpp
#include "Burrito.h"
#include <iostream>
Burrito::Burrito(){
}
Compile & Link :
lzsb$ g++ main.cpp -o main
Undefined symbols for architecture x86_64:
"Burrito::Burrito()", referenced from:
_main in ccVpCr0z.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
lzsb$
Platform:
Mac OS 10.6.8
G++ : i686-apple-darwin10 --with-gxx-include-dir=/usr/include/c++/4.2.1
You need to compile the Burrito.cpp file as well. The compiler creates object files from each .cpp file and links them afterwards. This is where your call fails, because the linker can't find the referenced Burrito class in any of your object files. To fix your compiler call just add Burrito.cpp
g++ main.cpp Burrito.cpp -o main
Your compile line should be:
g++ Burrito.cpp main.cpp -o main