Can't link other projects in my solution with premake - c++

I'm trying to get started with premake but I can't get my test project to link properly with it. If I link it manual it works fine though.
I'm using premake 4.3 (also tested it with premake 4.4) on OS X 10.9 with clang 3.4.
After I create a makefile via "premake4 gmake" and try to compile it I get an error like this:
Linking subproject
ld: internal error: atom not found in symbolIndex(__ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc) for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[1]: *** [libsubproject.dylib] Error 1
make: *** [subproject] Error 2
My pretty simple project setup:
project/
src/
test.cpp
subproject/
include/
Library.hpp
source/
Library.cpp
premake4.lua
premake4.lua
solution "testa"
configurations {"debug"}
language "C++"
includedirs {"subproject/include"}
project "subproject"
kind "SharedLib"
files {"subproject/source/*.cpp"}
project "main"
kind "ConsoleApp"
files {"src/*.cpp"}
links {"subproject"}
src/test.cpp
#include <iostream>
#include <Library.hpp>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
Library lib(13, 3);
lib.do_stuff(7);
return 0;
}
subproject/include/Library.hpp
#ifndef __LIBRARY_HPP__
#define __LIBRARY_HPP__
#include <iostream>
using namespace std;
class Library {
public:
Library(int, int);
void do_stuff(int) const;
private:
int x;
int y;
};
#endif
subproject/source/Library.cpp
#include <Library.hpp>
Library::Library(int x, int y) {
this->x = x;
this->y = y;
}
void Library::do_stuff(int z) const {
cout << "X: " << x << "Y: " << y << "Z: " << z << endl;
}
Thank you for your time.

This is a known premake bug. It was reported and fixed, but a fixed version of the program has not been released yet. See the discussion here.
This bug is caused by -Wl,-x linker flags that premake will add by default to the project.make makefile. As of now, there are two possible solutions, download the updated premake source with the fix, compile it and install the new version, or, manually change the value of LDFLAGS in the generated project.make after each run of premake.
I have also tried the suggestion they give in the link above of setting premake.tools.gcc.ldflags.flags._Symbols to nil, but it had no effect on my system.

Related

Intel MKL ERROR: Parameter 7 was incorrect on entry to DGELSD when including both Armadillo and Open3D in cpp

I am working on a cpp-project based on Open3D and armadillo on Ubuntu18.04. After adding Open3D::Open3D to linked libraries, some functions from armadillo like arma::inv(A) and arma::solve(X, A, B) that requires Intel MKL don't work and output error message like
"Intel MKL ERROR: Parameter 7 was incorrect on entry to DGELSD".
I've built a standalone program running linear regression that only calls functions in armadillo, but still when I include Open3D in CMakeLists, the program fails with the same error. I am wondering is there any compile flag I should specify when building Open3D, or any possible reason for it cause?
CPP program:
#include <iostream>
#include <armadillo>
#include <unordered_map>
#include <utility>
using namespace std;
using namespace arma;
using namespace std::chrono;
#define AVG_LEN (5ULL)
int main(int argc, char const *argv[])
{
std::unordered_map<std::string, std::pair<size_t, arma::dmat::fixed<AVG_LEN, 3ULL>>> upload_info;
upload_info["A1b"].second.fill(1.0);
auto it = upload_info.find("A1b");
for (int i=0; i<AVG_LEN; ++i) {
it->second.second(i, 0) = i*2;
it->second.second(i, 2) = i+10;
it->second.first++;
}
arma::vec2 X;
arma::dmat::fixed<AVG_LEN, 3ULL> info(it->second.second);
bool status = arma::solve(X, info.cols(0, 1), info.col(2));
if (status)
cout << "X: \n" << X << endl;
return 0;
}
Without including Open3D::Open3D in target_link_libraries, the output is correct as
X:
0.5000
10.0000
But after including that, it just outputs an error message
Intel MKL ERROR: Parameter 7 was incorrect on entry to DGELSD.
I am using Ubuntu 18.04 and Open3D 0.16.1. I build Open3D from source by
cmake -DBUILD_SHARED_LIBS=OFF \
-DBUILD_GUI=OFF -DBUILD_WEBRTC=OFF -DBUILD_PYTHON_MODULE=OFF \
-DGLIBCXX_USE_CXX11_ABI=ON -DWITH_OPENMP=ON \
-DCMAKE_INSTALL_PREFIX=$O3D_INSTALL_DIR ..
Additional information
Armadillo can be easily installed by apt-get install -y liblapack-dev libarmadillo-dev, it uses Blas to accelerate computation. I include Open3d in CMakeLists by
foreach(_target
demo) #
add_executable(${_target} "${_target}.cpp")
target_link_libraries(${_target} Open3D::Open3D
${ARMADILLO_LIBRARIES})
endforeach()
Finally resolved by setting -DBUILD_SHARED_LIBS=ON

boost::asio::ssl::context crash during construction

I can't figure why the following instruction crashes:
boost::asio::ssl::context ctx(boost::asio::ssl::context::tlsv12);
I got the following error: Process returned -1073741819 (0xC0000005)
There is nothing more to catch regarding exceptions and AFAIK the boost documentation doesn't mention incompatibility issues between the boost and openssl versions.
my environment:
gcc from cygwin: C:\cygwin64\x86_64-w64-mingw32-g++.exe
linker options: -lws2_32 -lcrypto -lssl
using boost 1.78 (dl from website) and cygwin's openssl 1.1.1m packages
here is the minimal example:
#include <iostream>
#include <boost/asio/ssl/context.hpp>
int main()
{
std::cout << "before" << std::endl;
try {
boost::asio::ssl::context ctx(boost::asio::ssl::context::tlsv12);
} catch (...) {
std::cout << "catch" << std::endl;
}
std::cout << "after" << std::endl;
return 0;
}
output:
before
The openssl cygwin package I installed is not a stable one, so the include and lib files are missing and I'm using the wrong ones (incompatible with the x86_64-w64-mingw32-g++ compiler). I've installed another stable version and the desired files are available now.

VS Code C++ OOP don't work Mac OS High Sierra

hello guys i'm new to vs code and i couldn't find a solution to use object oriented programming
when I create a .h file to call an object function I get an error
123MacBook-Pro-de-Rogerio: life DJMatrix $ cd "/ Users / DJMatrix / Documents / Classes / c ++ / life /" && g ++ main.cpp -o main && "/ Users / Dtrix / Documents / Classes / c ++ / life / "main
Undefined symbols for architecture x86_64:
  "Life :: tryAgain ()", referenced from:
      _main in main-ea3ce4.o
ld: symbol (s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
main.cpp:
#include <iostream>
#include "life.h"
using namespace std;
int main()
{
    Life life;
    life.tryAgain();
    return 0;
}
life.h:
#include <iostream>
using namespace std;
class Life
{
public:
    bool sucess;
  void tryAgain();
  void improve();
};
life.cpp:
#include "life.h"
void Life::tryAgain()
{
cout << "Trying again!!!" << endl;
}
void Life::improve()
{
cout << "Improve !!" << endl;
}
From what I see from the VSCode terminal, only main.cpp is getting compiled. The object file for life.cpp is not getting linked when you produce the final binary, that's why it's complaining that the Life::tryAgain() symbol is missing.
This depends on whether you're invoking the compiler manually or using Makefiles or letting VSCode do all of this for you; regardless the compile command should look like:
g++ -o main life.cpp main.cpp

C++ CMake not referencing function with .h properly

SOLUTION:
Just don't be a noob and run make in the same directory as cmake
Thanks for your answers!
I'm starting with C++ programming and I'm trying to understand how to properly reference a function with .h and .cpp file. I have following files:
\\func.h
#ifndef FUNC_H
#define FUNC_H
int charout(int a, char b);
#endif
\\func.cpp
#include <iostream>
#include "func.h"
using namespace std;
int charout(int a, char b)
{
cout << a;
cout << b;
return 0;
}
\\main.cpp
#include <iostream>
#include "func.h"
using namespace std;
int main ()
{
int a; char b;
cout << "insert an integer " << endl;
cin >> a;
cout << "insert a letter " << endl;
cin >> b;
charout(a,b);
return 0;
}
I am compiling using CMake (with func.h in folder 'include') with following structure:
# Declare the version of the CMake API for forward-compatibility
cmake_minimum_required(VERSION 2.8)
# Declare the name of the CMake Project
project(manual)
# Add the directory to search for header files
include_directories(include)
# Define an executable target
add_executable(main func.cpp main.cpp)
When I try to make main.cpp I am receiving an error:
make main g++ main.cpp -o main /tmp/cctlsXUG.o: In function
main': main.cpp:(.text+0x80): undefined reference tocharout(int,
char)' collect2: error: ld returned 1 exit status : recipe
for target 'main' failed make: *** [main] Error 1
Can you please take a look and let me know where am I doing a mistake?
I will appreciate your feedback. I'm really stuck with this. Cheers!
UPDATE:
OK I managed to compile it with g++ func.cpp main.cpp - o main when I was trying to compile the same code with gcc I got errors of sort undefined reference to std::cout'
I've found out that gcc does not give an access to std of C++. Can I somehow fix my CMake files to use g++ instead of gcc? The project I have to deliver is supposed to have a CMakeLists.txt included.
Finally the last question: why does CMake works fine with function declaration and main in the same file, but not when I split the function into header, cpp and main?

CodeBlocks with gmp, segfault with << operator and mp*_class

I am using CodeBlocks with gcc 4.7.2 and gmp 5.0.5 on a Win 7 64 machine.
After starting to use gmpxx I see a strange segfault which doesn't occur with the +,-,etc operators, but with << when trying to cout an mp*_class variable.
I have to say that gmpxx works perfectly so far except for this.
For example:
#include <iostream>
#include <gmpxx.h>
using namespace std;
int main()
{
mpz_class c = 21;
cout << c << endl;
}
gives a segfault on the line with the cout, whereas the below code works fine:
#include <iostream>
#include <gmpxx.h>
using namespace std;
int main()
{
mpz_class a = 3, b = 8, c;
c = a + b;
cout << c.get_str() << endl;
}
What is even stranger is that this code:
#include <iostream>
#include <gmpxx.h>
using namespace std;
int main()
{
mpz_class a = 3, b = 8, c, d = 21;
c = a + b;
cout << c.get_str() << endl;
cout << d << endl;
}
doesn't segfault when run, but shows only the first result (11) and then exits normally.
On the other hand in debug it segfaults on: cout << d << endl.
I've googled for the last couple of days and found nothing similar to only some of the overloaded operators not working.
I would be thankful for an explanation.
I linked the two gmp libraries in codeblocks like this:
Settings->Compiler and Debugger->Global Compiler Settings->Linker Settings
and there I've added: C:\mingw\lib\libgmpxx.dll.a and C:\mingw\lib\libgmp.dll.a (in that order).
Nothing else was needed in order to compile c++ code with gmpxx.
Finally my CodeBlocks build log looks like this:
g++.exe -pg -g -pg -g -c "C:\Temp\test.cpp" -o .objs\test.o
g++.exe -o test.exe .objs\test.o -pg -lgmon -pg -lgmon C:\mingw\lib\libgmpxx.dll.a C:\mingw\lib\libgmp.dll.a
I honestly don't know why there are two switches of each.
If you need any more info, I'd be glad to provide. Thank you.
Alright, time to mark this as answered. The fact is that I installed gmp via mingw-get whereas almost everywhere in googleland it states to build it yourself for your own system. A silly mistake and thanks to the comment by #Lol4t0 it works fine now.
So for all the new guys like me:
1) Install MinGW with MSYS
2) Download gmp source and extract to some folder in mingw\msys\1.0\home\
3) open mingw shell and navigate to gmp folder
4) ./configure --enable-cxx --prefix=/home/newgmpinstall
5) make
6) make install
7) make check
If it checks ok then in newgmpinstall you'll find the headers gmp.h and gmpxx.h and libraries libgmp.a and libgmpxx.a which work for your system.
You can move them to a new folder if you wish. Then in your IDE Project properties add the *.a files to your link libraries and the folder with the *.h files to the compiler search directories.
Write code
Note: At first ./configure exited with an error about M4 missing because I was missing the M4 package. Just download the source for M4 and do the above steps first for M4 and then install gmp.