Fail to compile source with JACK API - c++

For reference: i am currently on Manjaro (Arch Linux) and am using the GNU g++ compiler. I want to create a program for JACK (Jack Audio Connection Kit) and am now trying to use their API provided here. I have Jack2 installed, but also tried it with Jack2-dbus. I found something in the AUR called jackcpp, but that didn't help neither.
To get to my problem: I fail to compile their example clients listed, and obviously I fail to build my own using that API. I suspect it is broken, but I can't imagine noone reporting it so I don't really know what to do now.
In my jacktest.cpp the following is written:
#include <iostream>
#include "jack/control.h"
int main(){
jackctl_server_t *server;
jackctl_driver_t *driver;
if (jackctl_server_start(server, driver)) {
std::cout << "Started!";
return 0;
}else{
std::cout <<"Failed!";
return 1;
};
}
based on the function documented here. Try to start a jack server and return a bool if operation successful or not.
If I try to compile this (with 'pkg-config' as I am supposed to):
$ g++ -o jacktest `pkg-config --cflags --libs jack` jacktest.cpp
it throws:
jacktest.cpp:8:44: error: too many arguments to function 'bool jackctl_server_start(jackctl_server_t*)'
if (jackctl_server_start(server, driver))
So I checked and indeed, in my /usr/include/jack/control.h, it defines jackctl_server_start with only one argument. I don't know if their documentation is outdated or if I am missing files/have the wrong ones...
Then I tried to do it with only one argument
...
if (jackctl_server_start(server)) {
...
which then throws:
/usr/bin/ld: /tmp/ccxm3fWC.o: undefined reference to symbol '_ZNSt8ios_base4InitD1Ev##GLIBCXX_3.4'
/usr/lib/libstdc++.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
I tried to rearrange the arguments:
$ g++ -o jacktest `pkg-config --cflags --libs jack` jacktest.cpp
$ g++ -o jacktest jacktest.cpp `pkg-config --cflags --libs jack`
$ g++ jacktest.cpp `pkg-config --cflags --libs jack` -o jacktest
$ g++ jacktest.cpp -o jacktest `pkg-config --cflags --libs jack`
which always throws the same error...
now the thing that really makes me think something is broken: another jacktest_2.cpp now including jack.h and excluding control.h
#include <iostream>
#include "jack/jack.h"
int main(){
jack_client_t *client;
if (client = jack_client_new("test_client")) {
std::cout << "Running!";
return 0;
}else{
std::cout <<"Not Running!";
return 1;
};
}
which I CAN compile:
$ g++ -o jacktest_2 jacktest_2.cpp `pkg-config --cflags --libs jack`
altough it gives me complains that this function is depricated, the program does what it should too! So at least some of it is working?!
Also:
$ g++ -print-file-name=jack
/usr/lib/gcc/x86_64-pc-linux-gnu/7.2.1/../../../../lib/jack
is that normal? There is no lib/jack in usr/lib/gcc/x86_64-pc-linux-gnu/7.2.1
To conclude: I really NEED control.h, I even went as far as changing control.h and include their 'driver' part from the API Docs back into the function, but that didn't get me anywhere neither... (undefined reference...) I feel like I am missing something really simple, or something really is broken with that API. I have been on this for almost a week, and I just can't figure it out.

If anyone stumbles upon this and wondered what happend: I posted this in the JackAudio forum here. Anyone can read it if he/she wants to, but I will mark this question as answered now, since it really is not a C++ or compiling problem but rather a Jack problem. Thank you!

Related

resolving undefined references xcb

I can include items from xcb/xcb.h, but not items that are outlined in /usr/include/xcb/randr.h.
My preference is to use C++, but to help debug I also tried C which produced variations of the same error.
I am certain I am doing something incorrectly, but I am not sure where to start looking to resolve this. Thank you very much for reading, any suggestions?
Example
main.cpp
#include <xcb/xcb.h>
#include <xcb/randr.h>
int main()
{
const xcb_setup_t * xsetup;
xcb_connection_t * conn;
xcb_screen_t * screen;
xcb_window_t root_win;
xcb_screen_iterator_t screen_iterator;
xcb_randr_get_screen_resources_cookie_t resources;
// connect to Xserver
conn = xcb_connect(NULL, NULL);
xsetup = xcb_get_setup(conn);
// get the root window
screen_iterator = xcb_setup_roots_iterator(xsetup);
screen = screen_iterator.data;
root_win = screen->root;
// any function from xcb/randr.h fails with undefined reference.
resources = xcb_randr_get_screen_resources(conn, root_win);
}
Compile
# gcc tries
gcc -Wall main.cpp -o main `pkg-config --cflags --libs xcb`
g++ -Wall main.cpp -o main `pkg-config --cflags --libs xcb`
# clang tries
clang++ main.cpp -o main `pkg-config --cflags --libs xcb`
clang main.cpp -o main `pkg-config --cflags --libs xcb`
Result
gcc
/usr/bin/ld: /tmp/ccWR2GQL.o: in function `main':
main.cpp:(.text+0x6c): undefined reference to `xcb_randr_get_screen_resources'
collect2: error: ld returned 1 exit status
clang
/usr/bin/ld: /tmp/main-d114b5.o: in function `main':
main.cpp:(.text+0x67): undefined reference to `xcb_randr_get_screen_resources'
clang-7: error: linker command failed with exit code 1 (use -v to see invocation)
xcb libraries are split up in several different packages; so it goes that you need to pull in both xcb and xcb-randr libraries, explicitly:
... `pkg-config --cflags --libs xcb xcb-randr`
It's possible that your Linux distribution packages the randr library separately. Checking Fedora, it packages both xcb and xcb-rand in the libxcb-devel subpackage; but it's possible that your Linux distribution has a separate libxcb-randr-devel subpackage that you need to install.
Thank you very much n.m. and G.M. .
I was not linking the xcb-randr .
Solution:
clang++ main.cpp -o main `pkg-config --cflags --libs xcb` -lxcb-randr

Can't link GLFW library with it's header file on Ubuntu 18.04

I've installed the libglfw3-dev:amd64 package on Ubuntu using the standard sudo apt get etc. My following compiling line is:
g++ -o output -IL/usr/lib/x86_64-linux-gnu -lglfw driver.o
My current c++ file is:
#include <GLFW/glfw3.h>
int main(void)
{
GLFWwindow* window;
if (!glfwInit())
return -1;
}
I've tried using local libraries of glfw and setting the -I and -L locations but nothing has seemed to work. I've made sure the .so and .h files are in their respective locations but I always get this error while running make:
g++ -o output -I/usr/include/GLFW -L/usr/lib/x86_64-linux-gnu -lglfw
driver.o
driver.o: In function `main':
driver.cpp:(.text+0x5): undefined reference to `glfwInit'
collect2: error: ld returned 1 exit status
Makefile:2: recipe for target 'output' failed
make: *** [output] Error 1
I've tried looking at all the other SO posts and they recommend compiling with tons of extra flags, but the only thing I've been able to draw from them is that something is wrong with my library since VScode detects the .h files. How can I compile this without any errors?
Have you tried swapping the linker arguments around? That is, compile with
g++ -o output driver.o -lglfw
The linker goes through the files from left to right, and it has to know which symbols from libraries you need, before the libraries are processed.
All is perfectly explained in the manual https://www.glfw.org/docs/latest/build_guide.html#build_link_pkgconfig
The key problem is in your -I/usr/include/GLFW and #include <GLFW/glfw3.h> that gives in sum the path /usr/include/GLFW/GLFW/glfw3.h. I suppose this is a wrong path to glfw3.h. compilation was successful because of the system default include path -I/usr/include.
Do not tune compiler flags manually, let pkg-config do
it for you.
A typical compile and link command-line when using the static version of the GLFW library may look like this:
g++ -o output `pkg-config --cflags glfw3` yourprog.c `pkg-config --static --libs glfw3`
If you are using the shared version of the GLFW library, simply omit the --static flag.
g++ -o output `pkg-config --cflags glfw3` yourprog.c `pkg-config --libs glfw3`

GiNaC undefined reference

I am currently trying to develop a C++ application which will envolve solving some algebraic tasks (such as differentiation or integration) using GiNaC; I've installed it first from the Ubuntu Software Center (Ubuntu 13.04) and afterwards directly from the ftp ftp://ftpthep.physik.uni-mainz.de/pub/GiNaC/ ; however, everytime i try to compile the following example program:
#include <iostream>
#include <ginac/ginac.h>
using namespace std;
using namespace GiNaC;
int main()
{
symbol x("x"), y("y");
ex poly;
for (int i=0; i<3; ++i)
poly += factorial(i+16)*pow(x,i)*pow(y,2-i);
cout << poly << endl;
return 0;
}
i get a list of errors, all starting with "undefined reference to GiNaC::". I have verified that cln is also installed and the header files are on the default locations. Also, when compiling I've used the command g++ -o simple pkg-config --cflags --libs ginac simple.cpp
and g++ -o simple -lginac -lcln simple.cpp, both have failed to compile.
The problem is the order of the parameters on the compile line. Try one of the following two variants:
g++ -o simple simple.cpp `pkg-config --cflags --libs ginac`
g++ -o simple -Wl,--no-as-needed `pkg-config --cflags --libs ginac` simple.cpp
The idea is that the order of the object files and libraries is important to the linker. Very simply put, by default it only links a library if it needs it to resolve some previously unresolved symbols.
The first variant above moves the libraries at the end of the build parameters (so after the object file for your code), while the second variant disables this behavior in the linker.
I ran into the same exact issue, and I found 2 things that does bring you to where you want to be:
I placed -lcln after -lginac on the command line
This enabled me to compile it, but the program would not run. I found the solution to this. The error was "GLIB_CXX_3.4.21 not defined in libstdc++.so.6 with link time reference"
Link statically to libstdc++ with -static-libstdc++
As in:
g++ -g testgin.cpp -o simple -lcln -lginac -static-libstdc++
Peace

Allegro 5.0 - why am I getting linker errors?

I recently compiled Allegro-5.0 from source (following the instructions from the wiki). I didn't get any errors during the process, so I assume it went well. However, I am getting linker errors with even the most minimal test programs:
//test.cpp
#include <allegro5/allegro.h>
int main()
{
al_init();
return 0;
}
I am compiling with
g++ `pkg-config --libs allegro-5.0` test.cpp -o test
But I always get
/tmp/ccVmmERa.o: In function `main':
test.cpp:(.text+0xf): undefined reference to `al_install_system'
collect2: error: ld returned 1 exit status
(pkg-config returns -L/usr/local/lib -lallegro, there doesn't seem to be anything wrong with that)
Also, I can compile the examples with the the given makefile just fine, so there doesn't seem to be anything wrong with the library. I would just try to do the same thing the makefile does to make it work, but I don't understand it at all (I can barely write my own simple ones).
Oh, and I'm using Ubuntu 11.10 and gcc 4.7, though I don't think that has anything to do with it.
So - why does this happen, and how can I fix it?
Put the libs last on the command line:
g++ test.cpp -o test `pkg-config --libs allegro-5.0`
When linking, the order of the files and libraries matter.

Using exit() in c++

For one reason or another, I am messing around with the exit() function in c++. I am getting all kinds of strange errors from my mac running lion (64 bit). I am compiling using g++ -o -g -Wall.
Exhibit A:
#include <iostream>
int main(int arc, char *argv[]){
exit(1);
}
The Terminal output looks like this
$ g++ -o -g -Wall test main.cpp
ld: in test, can't link with a main executable for architecture x86_64
collect2: ld returned 1 exit status
but $ g++ -o test main.cpp compiles fine.
using #include<stdio.h> or #include<stdlib.h> result in the same compilation error.
I am just wondering if anyone might be able to see immediately what is going on here?
test is the name of the binary to produce, your first argument list should be:
> g++ -g -Wall -o test main.cpp
^^^^^^^ -o has test for an argument
-o is meant to be followed immediately by the name of the output file. It is probably trying to use your old binary 'test' as a source file, incorrectly.
Try this:
g++ -o test -g -Wall main.cpp