Cannot Configure CLion for Linking C++ - c++

I have a simple c++ example that I am trying to compile on a MacOSX machine using the CLion IDE. The goal is to use header files and cmake to compile this code on CLion with an aim to expand to something bigger and so for now, I have simplified my code to have a bar.h header file and a bar.cpp implementation file. I am getting compilation issues during linking. My code looks as follows:
main.cpp
#include "bar.h"
using namespace std;
int main(int argc, char** argv){
cout<<"starting"<<endl;
Bar bar = Bar("Using Bar Function From Main");
bar.foo();
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.21)
set(CMAKE_CXX_STANDARD 11)
project(librarytest)
add_executable(myprog main.cpp)
add_subdirectory(bars)
target_link_libraries(myprog PRIVATE bars)
bars\bar.h
#pragma once
#include <string>
#include <iostream>
class Bar{
private:
std::string s;
public:
Bar(std::string s);
void foo();
};
bars\bar.cpp
#include <string>
#include <iostream>
class Bar{
private:
std::string s;
public:
Bar(std::string s){
std::cout<<"Function Bar"<<std::endl;
this->s = s;
}
void foo(){
std::cout << this->s << std::endl;
}
};
bars\CMakeLists.txt
add_library(bars OBJECT
bar.cpp
)
target_include_directories(bars PUBLIC .)
I get the following error message from my CLion CMake build (I have obfuscated by personal details/directory):
====================[ Build | all | Debug ]=====================================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /{obfuscated-for-security}/testingincludes/cmake-build-debug --target all
[3/3] Linking CXX executable myprog
FAILED: myprog
: && /Library/Developer/CommandLineTools/usr/bin/c++ -g -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names bars/CMakeFiles/bars.dir/bar.cpp.o CMakeFiles/myprog.dir/main.cpp.o -o myprog && :
Undefined symbols for architecture x86_64:
"Bar::foo()", referenced from:
_main in main.cpp.o
"Bar::Bar(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
I have searched other Stack Overflow articles on similar errors but none can explain the issue as it pertains to linking on the OSX. I have followed some of the guidance like re-installing the compiler.
Can anyone suggest how to get this working on the MacOSX using CLion? Any pointers or suggestions would be welcomed.

The problem is not the build (AFAIK), it's the code.
Your bar.cpp file should look like this
#include "bar.h"
#include <string>
#include <iostream>
Bar::Bar(std::string s){
std::cout<<"Function Bar"<<std::endl;
this->s = s;
}
void Bar::foo(){
std::cout << this->s << std::endl;
}
Your version duplicates the Bar class definition in both the header and cpp file. My version includes the Bar class definition and just adds the definitions of the constructor and member function.

Related

Basic practice of dynamic library C++

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

Problem with compiling header and cpp file on macOS Catalina in clang++

I've been trying tu run this code but I'm keep getting this long error message. I think the code is okay, because it's working on my Windows computer, so I think it's a problem with my compiler. I am using VisualStudioCode on macOS Catalina 10.15.7 and clang 12.0.0.
This is my main.cpp file
#include<iostream>
#include <memory>
#include "Wezel.h"
using namespace std;
int main(){
for (int i = 0; i < 10; i++) {
unique_ptr<Wezel>(new Wezel());
}
return 0;
}
Wezel.cpp file
#include "Wezel.h"
#include <iostream>
using namespace std;
Wezel::~Wezel() {
cout << "Destruct" << endl;
}
Wezel.h file
#ifndef WEZEL_H
#define WEZEL_H
class Wezel
{
public:
~Wezel();
};
#endif
The error message I get when I try to compile the main.cpp file
majkel#mbp-micha RecyclingCpp % cd "/Users/majkel/Documents/Obiektowe/RecyclingCpp/" && g++ tempCodeRunnerFile.cpp -o tempCodeRunnerFile && "/Users/majkel/Documents/Obiektowe/RecyclingCpp/"tempCodeRunnerFile
Undefined symbols for architecture x86_64:
"Wezel::~Wezel()", referenced from:
std::__1::default_delete<Wezel>::operator()(Wezel*) const in tempCodeRunnerFile-40bbc5.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 didn't tell the compiler that Wezel.cpp was part of the thing you are trying to build, so it doesn't know where to find Wezel::~Wezel().
Try like this:
g++ main.cpp Wezel.cpp

"clang: error: linker command failed with exit code 1" on "ld: symbol(s) not found for architecture x86_64"

Right, please bare with on this, it might be quite a long one, and one related issue was solved here (I think): CMake make[2]: *** No rule to make target `/path/to/uthash/utarray.h', needed by `HelloTest'. Stop.
I have been struggling for some days now to build a simple 'Hello World' programme which mixes C and C++, and pulls in various external libraries using CMake. For full disclosure it should be known that I am fairly new to C, C++ and CMake hence please be nice.
I am working in OS X Yosemite 10.10.4 (my googling seems to suggest this might be part of the problem). I am working out of the CLion IDE.
Alas, here we go, here is the programme I am trying to build:
#include <iostream>
#include "Simbody.h" \\Written in C++
extern "C"
{
#include "project_in_C.h"
}
using namespace std;
using namespace SimTK;
int main(int argc, char** argv) {
cout << "Hello, World!" << endl;
return 0;
}
We have a physics library written in C++ a bespoke project written in C, for which the main header file is, lets call it; project_in_C.h.
Again; all I am trying to do is build a simple mixed C/C++ project.
Now, the above is executed using CMake, and the following CMakeLists.txt file:
cmake_minimum_required(VERSION 3.2)
project(HelloTest)
# Simbody
find_package(Simbody REQUIRED)
include_directories(${Simbody_INCLUDE_DIR})
link_directories(${Simbody_LIB_DIR})
# Project C Headers
set(PROJC_ROOT_DIR "/Users/usr/project-c")
set(PROJC_INCLUDE_DIR ${PROJC_ROOT_DIR}/src
${PROJC_ROOT_DIR}/dir1
${PROJC_ROOT_DIR}/dir2)
include_directories(${PROJC_INCLUDE_DIR})
# Check that it has found the most important header
find_path(projFound project_in_C.h PATHS "/Users/usr/project-c/src")
if(NOT projFound)
message(FATAL_ERROR "Cannot find folder containing project_in_C.h")
endif()
# Project C Source [we want to avoid globbing]
set(PROJC_SOURCE ${PROJC_ROOT_DIR}/src/file1.c
${PROJC_ROOT_DIR}/src/file2.c
${PROJC_ROOT_DIR}/src/file3.c
${PROJC_ROOT_DIR}/src/file4.c
${PROJC_ROOT_DIR}/dir1/file5.c)
# Make library from source files
add_library(PROJC_LIBRARIES ${PROJC_SOURCE})
# Tie together
set(SOURCE_FILES main.cpp)
add_executable(HelloTest ${SOURCE_FILES})
target_link_libraries(HelloTest ${Simbody_LIBRARIES} ${PROJC_LIBRARIES})
So far so good, but here is where the truly mysterious problem arises. Upon build this is what I get in return:
/Applications/CLion.app/Contents/bin/cmake/bin/cmake --build /Users/usr/Library/Caches/clion10/cmake/generated/c1d0f54d/c1d0f54d/Debug --target all -- -j 2
Scanning dependencies of target HelloTest
[ 91%] Built target PROJC_LIBRARIES
[100%] Building CXX object CMakeFiles/HelloTest.dir/main.cpp.o
Linking CXX executable HelloTest
Undefined symbols for architecture x86_64:
"_program_execution_wrapper", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [HelloTest] Error 1
make[1]: *** [CMakeFiles/HelloTest.dir/all] Error 2
make: *** [all] Error 2
But what the flippin' dynamite does that actually mean?
It is kicking up a fuss with project_in_C.h where the section the error seems to be referring to is written as so:
int program_execution_wrapper(int argc, char **argv);
int __program(int argc, char **argv);
#define main main(int argc, char **argv) { return program_execution_wrapper(argc, argv); } int __program
As ever, any help greatly appreciated.
Em...Undefined symbols is _program_execution_wrapper ... I don't see this symbol anywhere in you program. Is this maybe typo in your post? what if you change code in project_in_C.h from prog_exec_wrapper to program_execution_wrapper? And also, this function is not implemented in your code. For a start try something like this:
int program_execution_wrapper(int argc, char **argv) { printf("%s", "something"); }
Edit based on your comments:
See the following example:
We have two files (main.cpp)
#include <iostream>
int sum(int a, int b);
int main()
{
int a = sum(3, 5);
std::cout << a << std::endl;
return 0;
}
and file sum.cpp
#include <iostream>
int sum(int a, int b)
{
return a+b;
}
So I've I would like to compile this program...it would be like this
g++ main.cpp
I get the following error:
Undefined symbols for architecture x86_64:
"sum(int, int)", referenced from:
_main in main-22f955.o
ld: symbol(s) not found for architecture x86_64
But if I include sum.cpp in my main program, then program will work as charm.

link error when accessing std::string members on Mac Yosemite with libc++

I'm trying to compile this kind of code and I get link errors when trying to build using latest Xcode (6.0) on MacOSX Yosemite:
#include <iostream>
#include <string>
int main()
{
auto x = &std::string::size;
std::string hello("hello");
std::cout << (hello.*x)() << std::endl;
return 0;
}
$ g++ --std=c++11 -stdlib=libc++ hello.cpp -o hello
KO:
Undefined symbols for architecture x86_64:
"std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::size() const", referenced from:
_main in hello-a9a7cd.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Note that the code compiles file using libstdc++:
$ g++ --std=c++11 -stdlib=libstdc++ hello.cpp -o hello
OK
I was having the same problem. See Taking pointer to member std::string::size fails to link with libc++ but works with libstdc++
Basically the solution I came up with is to force the instantiation of the std::string template class.
Here is the proposed solution:
#include <iostream>
#include <string>
template class std::basic_string<char>;
int main()
{
auto x = &std::string::size;
std::string hello("hello");
std::cout << (hello.*x)() << std::endl;
return 0;
}

C++ class method not found when compiled

I created a simple class 'Hello' in C++ using header(.h) and definition(.cpp) files. This is the header file content:
#ifndef HELLO_H
#define HELLO_H
#include <string>
namespace test
{
class Hello
{
private:
std::string name;
public:
Hello();
void say_hello();
};
}
#endif
And the definition file content is just as you expected:
#include "Hello.h"
#include <iostream.h>
using namespace test;
Hello::Hello()
{
this->name = "Yoppy Yunhasnawa";
}
void Hello::say_hello()
{
string message = "Hello, " + this->name + ".. Have nice day!";
cout << message << "\n";
}
I included this class to a main.cpp file and use it like this:
#include "Hello.h"
using namespace test;
int main(int argc, char *argv[])
{
Hello* hello = new Hello;
hello->say_hello();
}
When I compiled the main.cpp file with g++ like this,
g++ main.cpp
I got following annoying error:
Undefined symbols for architecture x86_64:
"test::Hello::say_hello()", referenced from:
_main in ccsaoOZa.o
"test::Hello::Hello()", referenced from:
_main in ccsaoOZa.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
However, that error does not appear when I don't call both constructor and say_hello method:
int main(int argc, char *argv[])
{
Hello* hello;// = new Hello;
//hello->say_hello();
}
I use macport GCC 4.7 and I am very sure that my method is there but why this symbol(s) not found error keep appearing? Please show me my mistake. Thank you.
When you invoke g++ main.cpp, compiler performs both compiling AND linking. But the code cannot be linked without Hello.cpp file. So, you have two options: either compile and link separately:
g++ -c main.cpp
g++ -c hello.cpp
gcc main.o hello.o
or compile and link everything at the same time:
g++ main.cpp hello.cpp